php.net |  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
 [2011-12-15 15:15 UTC] aron dot cederholm at gmail dot com
Description:
------------
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 http://pastebin.com/gq2qekYh

$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
die();

## while, these will work as expected

# 1
foreach ($array as $a) {}
print_r($array);
die();

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

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

# 4
foreach ($array as $a) {}
foreach ($array as $a) {}
print_r($array);
die();

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

?>

Expected result:
----------------
a,b,c,d

Actual result:
--------------
a,b,c,c

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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: http://www.php.net/manual/en/control-structures.foreach.php
 [2011-12-15 18:02 UTC] cataphract@php.net
-Status: Open +Status: Duplicate
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Jul 24 07:01:26 2019 UTC