Bug #76336 [NEW]: array foreach by reference will change the last value

From: Date: Sun, 13 May 2018 15:27:42 +0000
Subject: Bug #76336 [NEW]: array foreach by reference will change the last value
Groups: php.standards 
Request: Send a blank email to standards-+get-1329@lists.php.net to get a copy of this message
From: janpoem at gmail dot com Operating system: windows, ubuntu PHP version: 7.0.30 Package: PHP Language Specification Bug Type: Bug Bug description:array foreach by reference will change the last value Description: ------------ Foreach an array by reference will change the last value (previous). Test script: --------------- <?php $array = [ ['a' => 'aa'], ['b' => 'bb'], ['c' => 'cc'], ['d' => 'dd'], ['e' => 'ee'], ['f' => 'ff'], ]; foreach ($array as & $item) { } var_dump($array); /* here will output: array(6) { [0]=> array(1) { ["a"]=> string(2) "aa" } [1]=> array(1) { ["b"]=> string(2) "bb" } [2]=> array(1) { ["c"]=> string(2) "cc" } [3]=> array(1) { ["d"]=> string(2) "dd" } [4]=> array(1) { ["e"]=> string(2) "ee" } [5]=> &array(1) { ["f"]=> string(2) "ff" } } */ echo '=====================================', PHP_EOL; foreach ($array as $item) { var_dump($item); } /* array(1) { ["a"]=> string(2) "aa" } array(1) { ["b"]=> string(2) "bb" } array(1) { ["c"]=> string(2) "cc" } array(1) { ["d"]=> string(2) "dd" } array(1) { ["e"]=> string(2) "ee" } array(1) { ["e"]=> string(2) "ee" // => this one was changed. } */ -- Edit bug report at https://bugs.php.net/bug.php?id=76336&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76336&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76336&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76336&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=76336&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=76336&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=76336&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=76336&r=needscript Try newer version: https://bugs.php.net/fix.php?id=76336&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=76336&r=support Expected behavior: https://bugs.php.net/fix.php?id=76336&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=76336&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=76336&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=76336&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76336&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=76336&r=dst IIS Stability: https://bugs.php.net/fix.php?id=76336&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=76336&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=76336&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=76336&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=76336&r=mysqlcfg

« previous php.standards (#1329) next »