I've stepped upon one very interesting issue with foreach cycles in PHP. I have an array and two (quite similar) foreach cycles declared in one scope.
The interesting thing is that the second foreach doesn't work properly if it uses the same variable name as the first one and (at the same time) if the first one uses references (to the array items).
E.g.
$my_array = array("one", "two", "three", "four");
foreach($my_array as &$my_item) {
}
foreach($my_array as $my_item) {
$second_array[] = $my_item;
}
Then the $second_array
array doesn't contain "one", "two", "three", "four"
but actually "one", "two", "three", "three"
.
Could someone explain to me why that is? Because I have honestly no idea, the two variables should have different scopes, it works just fine when no references are used...
//The fix is quite simple, apart from the obvious merging the two foreach cycles into just one , it also helps to change the 'item property' name of one of the foreach cycles (e.g. $my_item_1). The thing is, I don't want to fix it, I want to understand it :).
EDIT: This works fine.
$my_array = array("one", "two", "three", "four");
foreach($my_array as $my_item) {
}
foreach($my_array as $my_item) {
$second_array[] = $my_item;
}
This works fine as well.
$my_array = array("one", "two", "three", "four");
foreach($my_array as &$my_item) {
}
foreach($my_array as $my_item_1) {
$second_array[] = $my_item_1;
}