Bug #76336 [Opn->Nab]: array foreach by reference will change the last value
[email protected] Sun, 13 May 2018 16:10:29 GMT
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=76336&edit=1 ID: 76336 Updated by: [email protected] Reported by: janpoem at gmail dot com Summary: array foreach by reference will change the last value -Status: Open +Status: Not a bug Type: Bug Package: PHP Language Specification Operating System: windows, ubuntu PHP Version: 7.0.30 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php In specific the part about foreach and using references http://php.net/manual/en/control-structures.foreach.php Previous Comments: ------------------------------------------------------------------------ [2018-05-13 15:27:40] janpoem at gmail dot com 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 this bug report at https://bugs.php.net/bug.php?id=76336&edit=1