Why is an empty foreach loop can change the result.
I have the following code:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
var_dump($variable);
The result I get is:
array (size=4)
0 => int 2
1 => int 3
2 => int 4
3 => &int 5
Now, when I add an empty foreach loop like this:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
foreach ($variable as $key => $value);
var_dump($variable);
I get this :
array (size=4)
0 => int 2
1 => int 3
2 => int 4
3 => &int 4
can someone explain me why the last element doesn't change when I add the second empty loop, and why there is a & infront of the last element?
At the end of the first loop, $value is pointing to the same place as $variable[3] (they are pointing to the same location in memory):
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
Even as this loop is finished, $value is still a reference that's pointing to the same location in memory as $variable[3], so each time you store a value in $value, this also overwrites the value stored for $variable[3]:
foreach ($variable as $key => $value);
var_dump($variable);
With each evaluation of this foreach, both $value and $variable[3] are becoming equal to the value of the iterable item in $variable.
So in the 3rd iteration of the second loop, $value and $variable[3] become equal to 4 by reference, then during the 4th and final iteration of the second loop, nothing changes because you're passing the value of $variable[3] (which is still &$value) to $value (which is still &$value).
It's very confusing, but it's not even slightly idiosyncratic; it's the code executing exactly as it should.
More info here: PHP: Passing by Reference
To prevent this behavior it is sufficient to add an unset($value); statement after each loop where it is used. An alternative to the unset may be to enclose the foreach loop in a self calling closure, in order to force $value to be local, but the amount of additional characters needed to do that is bigger than just unsetting it:
(function($variable){
foreach ($variable as $key => &$value) $value++;
})($variable);
This is a name collision: the name $value introduced in the first loop exists after it and is used in the second loop. So all assignments to it are in fact assignments to the original array. What you did is easier observed in this code:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
$value = 123; // <= here you alter the array!
var_dump($variable);
and you will see $variable[3] as 123.
One way to avoid this is, as others said, to unset ($value) after the loop, which should be a good practice as recommended by the manual. Another way is to use another variable in the second loop:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
foreach ($variable as $key => $val);
var_dump($variable);
which does not alter your array.
The last element of the array will remian even after the foreach loop ..So its needed to use unset function outside the loop ..That is
$variable = [1,2,3,4];
foreach ($variable as $key => &$value) {
$value++;
}
unset($value);
var_dump($variable);
The link to the manual can be found here http://php.net/manual/en/control-structures.foreach.php
As phil stated in the comments:
As mentioned in the manual, you should unset() references after use.
$variable = [1,2,3,4];
foreach ($variable as $key => &$value) {
$value ++;
}
unset($value);
foreach ($variable as $key => $value);
print_r($variable);
Will return:
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
Example
Explanation
Taken from the foreach() manual. (See the big red box)
Reference of a $value and the last array element remain even after the
foreach loop. It is recommended to destroy it by unset().
It basically means: That the referenced value &$value and the last element/item in the array, which in this case is 4 remain the same. To counter-act this issue, you'll have to unset() the value after use, otherwise it will stay in the array as its original value (if that makes sense).
You should also read this: How does PHP 'foreach' actually work?
After loop you should unset this reference using:
unset($value);
So your whole code should work like this:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value) {
$value++;
}
unset($value);
var_dump($variable);
There is no point to put unset($value); inside the loop
Explanation - after loop $value is still set to the last element of array so you can use after your loop $value = 10; (before unset) and you will see that last element of array has been changed to 10. It seems that var_dump want to help us a bit in this case and shows there is reference for last element and of course when we use unset we have desired output of var_dump.
You could also look at the following script:
<?php
$array = [1, 2, 3, 4];
var_dump($array);
$x = &$array[2];
var_dump($array);
$x += 20;
unset($x);
var_dump($array);
?>
We don't use loop here and if reference is set to element of array, var_dump shows us this putting & before type of this element.
However if the above code we changed reference and set it this way $x = &$array; var_dump wouldn't show us any reference.
Also in the following code:
<?php
$x = 23;
$ref = &$x;
var_dump($x);
?>
var_dump() won't give us any hint.
Obligatory statement: References are evil!
Stepping through your code:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value++;
After the loop completes; $value is a reference to $variable[3] and thus has the value of int(4).
foreach ($variable as $key => $value);
At each iteration, $variable[3] gets assigned an element of $variable[<k>] where 0 <= k < 3. At the last iteration it gets assigned to its own value which is that of the previous iteration, so it's int(4).
Unsetting $value in between the two loops resolves the situation. See also an earlier answer by me.
Related
$databaseObjects = Database::instance()->query('SELECT * FROM table');
foreach ($databaseObjects as $key => $value) {
echo "(key: $key, value: $value)";
if ($key + 1 < count($objects))
$nextObject = $objects[$key + 1];
}
Given an array of database objects from Kohana, the above loop will skip all the odd keys. The reason for this is because accessing $objects[$key+1] increments $key. I don't understand the reason behind this. Since when did the array access operator increment the key?
$objects[$key+1] does not increment $key
Your code is weird - you set $nextObject but then never use it.
Remember: array keys are NOT always in order such as 0,1,2,3,4,5!
For instance, this array:
<?php
$arr = array(
0 => "Zero",
2 => "Two",
5 => "Five"
);
?>
Would print out the following in your script:
(key: 0, value: Zero)(key: 2, value: Two)(key: 5, value: Five)
... and this array:
<?php
$arr = array(
5 => "Five"
2 => "Two",
0 => "Zero",
);
?>
Would print out the following in your script:
(key: 5, value: Five)(key: 2, value: Two)(key: 0, value: Zero)
$objects[$key + 1]; does not increment the $key, $key + 1 is not being assigned to $key. Moreover, even if $key was assigned $key + 1, this won't affect the $key variable in the next iteration since $key is local to the foreach and initialized every time the foreach iterate on the next element in the array.
However, since this is a foreach loop, the $key is nothing but the index (assuming this array is not an associative one). Therefore the $key will be incrementing starting from zero every time the loop iterate on an element.
It appears I left out the important detail that I am iterating over Kohana database objects. They must have overwritten the foreach iterator in a fashion such that when I perform an array access within the foreach loop,
$nextObject = $objects[$key + 1];
it will affect the $key on the next iteration.
for example:
$numbers = array(1, 2, 3, 4, 5);
foreach($numbers as $value)
{
echo $value;
}
what does as do, I assume it's a keyword because it is highlighted as one in my text editor. I checked the keyword list at http://www.php.net/manual/en/reserved.keywords.php and the as keyword was present. It linked to the foreach construct page, and from what I could tell didn't describe what the as keyword did. Does this as keyword have other uses or is it just used in the foreach construct?
It's used to iterate over Iterators, e.g. arrays. It reads:
foreach value in $numbers, assign the value to $value.
You can also do:
foreach ($numbers as $index => $value) {
}
Which reads:
foreach value in $numbers, assign the index/key to $index and the value to $value.
To demonstrate the full syntax, lets say we have the following:
$array = array(
0 => 'hello',
1 => 'world')
);
For the first iteration of our foreach loop, $index would be 0 and $value would be 'hello'. For the second iteration of our foreach loop, $index would be 1, and $value would be 'world'.
Foreach loops are very common. You may have seen them as for..in loops in other languages.
For the as keyword, you're saying:
for each value in $numbers AS $value
It's a bit awkward, I know. As far as I know, the as keyword is only used with foreach loops.
as is just part of the foreach syntax, it's used to specify the variables that are assigned during the iteration. I don't think it's used in any other context in PHP.
This may sound weird but since not an English native, I'm having a trouble grasping the semantics of 'as' in the code.
$array = array(1, 2, 3, 4);{
foreach ($array as &$value)
$value = $value * 2;}
I don't get the meaning of as. I get what the command line is producing, but it's confusing for me probably as some of this would be for you:
foreach ($array with &$value)
or
foreach ($array then &$value)
Can some native Englishman/lady explain me why was 'as' chosen for this purpose.
It means something like
foreach item in $array provide it to me as variable $value
Just look it up in the wiktionary:
In the manner or role specified.
The kidnappers released him as agreed. The parties were seen as agreeing on a range of issues. He was never seen as the boss, but rather as a friend.
So in the context of foreach it means: go through every element of the array and use it as the specified variable (foreach($array as $content)).
For example, if there are 3 items in $array with numeric index, then the as means each time through the loop $value equals the current item, so you can think of it as an alias for the current item:
//first iteration
$value = $array[0]
//second
$value = $array[1]
//third
$value = $array[2]
It is the same as:
for($key=0; $key<count($array); $key++) {
$value = $array[$key];
}
foreach($array as $key => $value) { }
NikiC stated in another thread:
Right before [a foreach] iteration the $array is "soft copied" for
use in foreach. This means that no actual copy is done, but only the
refcount of the zval of $array is increased to 2.
However, my test code is showing a different result:
$array = array(0, 1, 2);
xdebug_debug_zval('array'); // refcount=1, is_ref=0
// so far so good
foreach ($array as $key => $value) {
xdebug_debug_zval('array'); // refcount=3, is_ref=0
} // why is refcount 3 instead of 2?
Just by looking at the code, we can see at most two array variables.
Why is refcount 3?
Why isn't refcount 2 after foreach is run?
The xdebug_debug_zval() is looking at the $array variable and not the $key variable.
if you change your code to:
foreach ($array as $key => $value) {
echo $key . " : " . $values . "<br>";
//xdebug_debug_zval('array');
}
The correct values of the array will be returned. I don't have the xdebug function so I can't test what value you put in there.
I was writing a simple PHP page and a few foreach loops were used.
Here are the scripts:
$arrs = array("a", "b", "c");
foreach ($arrs as $arr) {
if(substr($arr,0,1)=="b") {
echo "This is b";
}
} // End of first 'foreach' loop, and I didn't use 'ifelse' here.
And when this foreach ends, I wrote another foreach loop in which all the values in the foreach loop was the same as in the previous foreach.
foreach ($arrs as $arr) {
if(substr($arr,0,1)=="c") {
echo "This is c";
}
}
I am not sure if it is a good practice to have two foreach loops with the same values and keys.
Will the values get overwritten in the first foreach loop?
It's OK until you start using references and then you can get strange behaviour, for example:
<?php
$array = array(1,2,3,4);
//notice reference &
foreach ($array as & $item) { }
$array2 = array(10, 11, 12, 13);
foreach ($array2 as $item) { }
print_r($array);
Outputs this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 13
)
Because the first foreach leaves $item as a reference to $array[3], $array[3] is sequentially set to each value in $array2.
You can solve this be doing unset($item) after the first foreach, which will remove the reference, but this isn't actually an issue in your case, as you are not using references with foreach.
Two notable notes from the documentation of foreach:
Note: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.
and
Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.
Because you're not writing to the array, only reading from it and printing stuff out, no values in the array will be changed.
You can save time looping through the array twice though by doing this:
$arrs = array("a", "b", "c");
foreach ($arrs as $arr) {
if(substr($arr,0,1)=="b") {
echo "This is b";
}
if(substr($arr,0,1)=="c") {
echo "This is c";
}
}
All you need is 1 foreach loop.
Or an even shorter approach to what Jon said, is this:
$arrs = array("a", "b", "c");
foreach ($arrs as $arr)
if ($arr != "a")
echo 'This is ' . $arr;
This is much easier and faster than using substr, since you are already using a foreach loop. And try not to pollute it with a ton of if statements, if you find yourself doing this, it's better to use a switch statement, much faster.
Cheers :)