|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60534 Iterating an array twice w and w/o references changes last item to second last
Submitted: 2011-12-15 15:15 UTC Modified: 2011-12-15 18:02 UTC
From: aron dot cederholm at gmail dot com Assigned:
Status: Duplicate Package: Arrays related
PHP Version: 5.3.8 OS: Arch Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: aron dot cederholm at gmail dot com
New email:
PHP Version: OS:


 [2011-12-15 15:15 UTC] aron dot cederholm at gmail dot com
Iterating an array twice, first time with references, second time *without* 
references, will trigger a bug in the second iteration where the last item of the 
array is overwritten by the second last item in the array. This seem to be the 
same bug as #46123, which was dismissed on erroneous grounds

Test script:
# For a more verbose look at this bug, watch

$array = array('a', 'b', 'c', 'd'); # testing array
foreach ($array as &$a) {}
foreach ($array as $a) {}
echo join(',', $array); # will produce a,b,c,c *not* a,b,c,d as expected

## while, these will work as expected

# 1
foreach ($array as $a) {}

# 2
foreach ($array as &$a) {}

# 3
foreach ($array as &$a) {}
foreach ($array as &$a) {}

# 4
foreach ($array as $a) {}
foreach ($array as $a) {}

# 5
foreach ($array as $a) {}
foreach ($array as &$a) {}


Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-15 17:09 UTC] anon at anon dot anon
This gets submitted as a bug about twice a month. It's still not a bug.

After the first loop finishes $a is a reference to the last element of the array. That means that during the second loop, the last value of the array is constantly being modified by the loop. Once it gets to the last element, 'd' has been overwritten already.

foreach ($array as &$a) {
	echo "($a): " . join(',', $array) . "\n";
foreach ($array as $a) {
	echo "($a): " . join(',', $array) . "\n";

(a): a,b,c,d
(b): a,b,c,d
(c): a,b,c,d
(d): a,b,c,d
(a): a,b,c,a
(b): a,b,c,b
(c): a,b,c,c
(c): a,b,c,c

As it says in the manual, unset() after foreach'ing by reference:
 [2011-12-15 18:02 UTC]
-Status: Open +Status: Duplicate
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Jul 19 23:01:25 2019 UTC