Perhaps I'm simply having trouble understanding how php handles arrays.
I'm trying to print out an array using a foreach loop. All I can seem to get out of it is the word "Array".
<?php
$someArray[]=array('1','2','3','4','5','6','7'); // size 7
foreach($someArray as $value){
echo $value;
?>
<br />
<?php
}
?>
This prints out this:
Array
I'm having trouble understanding why this would be the case. If I define an array up front like above, it'll print "Array". It almost seems like I have to manually define everything... which means I must be doing something wrong.
This works:
<?php
$someArray[0] = '1';
$someArray[1] = '2';
$someArray[2] = '3';
$someArray[3] = '4';
$someArray[4] = '5';
$someArray[5] = '6';
$someArray[6] = '7';
for($i=0; $i<7; $i++){
echo $someArray[$i]."<br />";
}
?>
Why won't the foreach work?
here's a link to see it in action >> http://phpclass.hylianux.com/test.php
You haven't declared the array properly.
You have to remove the square brackets: [].
<?php
$someArray=array('1','2','3','4','5','6','7'); // size 7
foreach($someArray as $value){
echo $value;
?> <br />
<?php
}
?>
Try:
<?php
$someArray = array('1','2','3','4','5','6','7'); // size 7
foreach($someArray as $value){
echo $value . "<br />\n";
}
?>
Or:
<?php
$someArray = array(
0 => '1',
'a' => '2',
2 => '3'
);
foreach($someArray as $key => $val){
echo "Key: $key, Value: $val<br/>\n";
}
?>
actually, you're adding an array into another array.
$someArray[]=array('1','2','3','4','5','6','7');
the right way would be
$someArray=array('1','2','3','4','5','6','7');
Related
Hello I am submitting a form with POST method and I want its contents to be echo'ed one by one apart from the last one. So far I am using
<?php foreach($_POST as $data){
echo $data;
}
?>
which displays the whole array of $_POST, how can I make it using common "for" loop to not echo the last item of the array? It doesnt seem to work
<?php
$length=count($_POST)-1;
for($i=0; $i<$length; $i++) {
echo $_POST[$i];
?>
<br>
<?php } ?>
I am getting 5 errors, undefined offset 0 through 4 where the echo line is present
Do the following:
<?php
$counter = 0;
$lastItemOrder = count($_POST);
foreach($_POST as $value) {
$counter++;
if( $counter !== $lastItemOrder) {
echo $value;
}?>
<br><?php
} ?>
Your loop for just get numerical index like $_POSR[0], $_POST[1]... This just would work if in the HTML the attribute name of the input elements be numerical also, like name="0" and so on.
foreach perform loop on array independently of index, numerical or string.
Try this:
<?php
$counter = 0;
$lastItemOrder = count($_POST);
foreach($_POST as $index => $value) {
$counter++;
if( $counter !== $lastItemOrder) {
echo $index . ": " . $value;
}?>
<br><?php
} ?>
ok now I get it, I didnt know the difference between associative and numeric arrays. I fixed it with an if statement
<?php
$arr1 = preg_split("/[;]+/", $data);
foreach ($arr1 as $arr1 => $value) {
echo '<td>';
echo $value;
echo '</td>';
if (key($value)==6)
{
echo '</tr>';
echo '<tr>';
}
}
?>
This is code I am using but the key function is not returning anything.
I want to get return the position of $value and want to excecute some code if it is divisible by 6.
How can I get the position of $value in $arr1?
You must use a different variable than $arr1 to store the key.
Use this instead, where $key will be the key:
<?php
$arr1 = preg_split("/[;]+/", $data);
foreach ($arr1 as $key => $value) {
echo '<td>';
echo $value;
echo '</td>';
if ($key==6)
{
echo '</tr>';
echo '<tr>';
}
}
?>
You can following example to get position of particular $value in array.
<?php
$value = 'green';
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key_of_particular_value = array_search($value, array);
//After perform above operation $key_of_particular_value = 2
//Below statement will check whether key is divided by 6 or not
if(($key_of_particular_value % 6) == 0)
{
//perform your operation
}
?>
To know more please refer following link-
http://php.net/manual/en/function.array-search.php
You have used same variable $arr1 for key and original array, try using another variable to get key(index)
foreach ($arr1 as $key => $value) {
I am trying to do a double array to echo out "brown dog" and "white cat". I got an "Array to string conversion" error on the line where I am trying to echo out the statement. How can I fix this error? Thanks in advance. Below is my code:
<?
$pet1 = "dog";
$pet2 = "cat";
$arrayvalue = array();
$arrayvalue[0] = array("brown, "white");
$arrayvalue[1] = array("$pet1", "$pet2");
foreach($arrayvalue as $array)
{
echo "$arrayvalue[0]   $arrayvalue[1] </br>";
}
?>
The only way I can see this working is with a for loop. If you need to keep the current structure as it is, do this:
for ($i=0; $i<2; $i++)
echo $arrayvalue[0][$i] . ' ' . $arrayvalue[1][$i] . '<br>';
Here is how I would rewrite the program, keeping the current array structure and using the above for loop to solve your problem:
$pet1 = 'dog';
$pet2 = 'cat';
$arrayvalue = array(
array('brown', 'white'),
array($pet1, $pet2)
);
for ($i=0; $i<2; $i++)
echo $arrayvalue[0][$i] . ' ' . $arrayvalue[1][$i] . '<br>';
Output:
brown dog
white cat
You need to edit the foreach loop a little. Also, there's a problem with your quotes above. Try:
<?
$pet1 = "dog";
$pet2 = "cat";
$arrayvalue = array();
$arrayvalue[0] = array("brown", "white");
$arrayvalue[1] = array("$pet1", "$pet2");
foreach( $arrayvalue[0] as $key => $color ) {
echo "$color   $arrayvalue[1][$key] </br>";
}
?>
Basically instead of iterating through $arrayvalue, you should be going through the one with the colors or the one with the pets. In my example, I go through the one with the colors, and then reference the one with the pets, using $key to keep them in sync.
If possible, I would recommend using two separate arrays instead of one, or naming it more logically:
$array['colors'] = array("brown", "white");
$array['pets'] = array("$pet1", "$pet2");
foreach( $array['colors'] as $key => $color ) {
echo "$color   $array['pets'][$key] </br>";
}
//or
$colors = array("brown", "white");
$pets = array("$pet1", "$pet2");
foreach( $colors as $key => $color ) {
echo "$color   $pets[$key] </br>";
}
You are referencing $arrayvalue inside your foreach loop, when you should be using $array
foreach($arrayvalue as $array)
{
echo "{$array[0]}   {$array[1]} </br>";
}
I have that code
$names=array('name1'=>'John','Bill');
$names = (object)$names;
If I want get name John, I do :
$names->name1;
But, How can I get name 'Bill' ?
This is actually a issue/bug. [ https://bugs.php.net/bug.php?id=45959 ]
It's best to avoid array-to-object casts & If you really need to do, Create a new instance of stdClass and then manually renaming the variable. (Not a right Solution & I'm sure You must be knowing this!)
<?php
$names=array('name1'=>'John','Bill');
$obj = new stdClass;
foreach ($names as $k => $v) {
if (is_numeric($k)) {
$k = "_{$k}";
}
$obj->$k = $v;
}
echo "<pre>";
print_r($obj);
echo "</pre>";
echo $obj->_0;
?>
First way
$names=array('name1'=>'John','name2'=>'Bill');
$names = (object)$names;
var_dump($names->name2);//Bill
Second way:
using the loop get all variables:
$names=array('name1'=>'John','Bill');
$names = (object)$names;
foreach ($names as $name) {
echo ($name);
}
list($names->name1,$names->name2)=array_values(array('name1'=>'John','Bill'));
echo $names->name2; // Bill
EDIT1
$names=array('name1'=>'John','Bill');
$names = (object)$names;
echo end($names); // Bill
EDIT2
$names=array('name1'=>'John','Bill');
$names = (object)$names;
echo next($names); // Bill
You can iterate through it:
foreach ($names as $name) {
var_dump($name);
}
output:
string 'John' (length=4)
string 'Bill' (length=4)
I am having a array like so and I am looping trough it like this:
$options = array();
$options[0] = 'test1';
$options[1] = 'test2';
$options[2] = 'test3';
foreach($options as $x)
{
echo "Value=" . $x ;
echo "<br>";
}
It outputs as expected:
Value=test
Value=test2
Value=test3
Now I want to add some options to my array and loop trough them:
$options = array();
$options['first_option'] = 'test';
$options['second_option'] = get_option('second_option');
$options['third_option'] = get_option('third_option');
foreach($options as $x)
{
echo "Value=" . $x ;
echo "<br>";
}
But it does not work as I want. Because it outputs:
Value=first_option
Value=second_option
Value=third_option
So now I do not know how to access stored values using foreach from these guys?
Something like:
Value=first_option='test'
So when I use print_r($options)
Output is:
Array
(
[first_options] => test
[second_option] =>
[third_option] =>
)
1
your loop should look like this:
foreach($options as $key => $val){
echo "Val: ".$val;
echo "<br/>";
}
Your code is working just as expected and producing the desired result. You must have something else changing the values in $options. Correction: now that I see your edit, your functions are not returning any values, so options 1 and 2 are blank. Make sure that function returns something. Other than that, all of this code is good.
By the way, I recommend this:
$options = [
'first_option' => 'test',
'second_option' => get_option('second_option'),
'third_option' => get_option('third_option')
];
foreach($options as $key) {
echo "Value = {$key}<br>";
}
you can also use:
foreach($options as $key => $value) {
echo "Value - {$value} = {$key}<br>";
}
or you could at least replace array() with just []. Those are just some suggestions for neatness.