I'm really getting crazy with this since two days now. I'd greatly appreciate if someone could give me a hint. I definitely can't understand why this PHP code:
$arr [] = [ "name" => "Chapter 1" ];
$arr [] = [ "name" => "Chapter 2" ];
foreach ( $arr as &$item )
echo $item['name']."<br>";
echo "============<br>";
foreach ( $arr as $item )
echo $item['name']."<br>";
gives this output:
Chapter 1
Chapter 2
============
Chapter 1
Chapter 1 (I would expect 'Chapter 2' here)
It looks like the first loop modifies the array, even though there is no assignment in the loop. Strangely enough, everything works as expected, when I remove the ampersand.
The thing I don't understand is, why is the array getting modified at all, even though I don't do anything with the reference variable '&$item' variable (except echoing it).
I also tried reset() between the loops. But it didn't change anything, and according to the manual it shouldn't be necessary anyway in such a case (at lease from my understanding) because the loops start after each other and are not nested somehow.
Thanks a lot!
Bernd