From: Operating system: RHEL 5.6 PHP version: 5.2.17 Package: Scripting Engine problem Bug Type: Bug Bug description:foreach changes array values after iterating by reference followed by foreach
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 ) -- Edit bug report at http://bugs.php.net/bug.php?id=54189&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=54189&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=54189&r=trysnapshot53 Try a snapshot (trunk): http://bugs.php.net/fix.php?id=54189&r=trysnapshottrunk Fixed in SVN: http://bugs.php.net/fix.php?id=54189&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=54189&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=54189&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=54189&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=54189&r=needscript Try newer version: http://bugs.php.net/fix.php?id=54189&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=54189&r=support Expected behavior: http://bugs.php.net/fix.php?id=54189&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=54189&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=54189&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=54189&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54189&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=54189&r=dst IIS Stability: http://bugs.php.net/fix.php?id=54189&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=54189&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=54189&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=54189&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=54189&r=mysqlcfg