|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-03-08 09:24 UTC] scottmac@php.net
-Status: Open
+Status: Bogus
[2011-03-08 09:24 UTC] scottmac@php.net
[2015-01-01 20:29 UTC] chealer at gmail dot com
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 08:00:01 2025 UTC |
Description: ------------ (I searched through the bug reports and found some foreach pointer issues, but none related to this) If I simply go through an array with a referenced value like so: foreach ($array as &$value) { } And then go through it again, not referenced, like so: foreach ($array as $value) { } Both times using the same variable $value, then during the second foreach, the second to the last value is actually copied over the last value changing the array. If I simply use $value2 there is no issue. Test script: --------------- $array = array('aaaaa','bbbbb','ccccc','ddddd','eeeee'); echo '1) '.print_r($array, true).'<br>'; foreach ($array as &$value) { } echo '2) '.print_r($array, true).'<br> 3) '; foreach ($array as $key => $value) { echo "[$key] => $value "; } echo '<br> 4) '.print_r($array, true).'<br>'; Expected result: ---------------- 1) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) 2) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) 3) [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee 4) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) Actual result: -------------- 1) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) 2) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) 3) [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => ddddd 4) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => ddddd )