|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-09-19 13:15 UTC] scottmac@php.net
[2008-09-19 13:39 UTC] martin dot akesson at qbrick dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 12:00:01 2025 UTC |
Description: ------------ Using a foreach loop to edit values by reference in an array will mangle the array. From testing seems the last item in the array will be replaced by the second last item. It seems the problem shows once the array has been traversed start to end. The code to reproduce the problem does a much better job describing the issue. Reproduce code: --------------- <?php $list = array('one', 'two', 'three', 'four'); foreach ($list as &$item) { $item = "Row $item"; } print_r($list); foreach ($list as $index => $item) { printf("Item #%u: %s\n", $index, $item); } print_r($list); ?> Expected result: ---------------- Array ( [0] => Row one [1] => Row two [2] => Row three [3] => Row four ) Item #0: Row one Item #1: Row two Item #2: Row three Item #3: Row four Array ( [0] => Row one [1] => Row two [2] => Row three [3] => Row four ) Actual result: -------------- Array ( [0] => Row one [1] => Row two [2] => Row three [3] => Row four ) Item #0: Row one Item #1: Row two Item #2: Row three Item #3: Row three Array ( [0] => Row one [1] => Row two [2] => Row three [3] => Row three )