Bug #60534 [NEW]: Iterating an array twice w and w/o references changes last item to second last

From: Date: Thu, 15 Dec 2011 15:15:12 +0000
Subject: Bug #60534 [NEW]: Iterating an array twice w and w/o references changes last item to second last
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-165886@lists.php.net to get a copy of this message
From:             
Operating system: Arch Linux
PHP version:      5.3.8
Package:          Arrays related
Bug Type:         Bug
Bug description:Iterating an array twice w and w/o references changes last item to second last

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

-- 
Edit bug report at https://bugs.php.net/bug.php?id=60534&edit=1
-- 
Try a snapshot (PHP 5.4):            https://bugs.php.net/fix.php?id=60534&r=trysnapshot54
Try a snapshot (PHP 5.3):            https://bugs.php.net/fix.php?id=60534&r=trysnapshot53
Try a snapshot (trunk):              https://bugs.php.net/fix.php?id=60534&r=trysnapshottrunk
Fixed in SVN:                        https://bugs.php.net/fix.php?id=60534&r=fixed
Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=60534&r=needdocs
Fixed in release:                    https://bugs.php.net/fix.php?id=60534&r=alreadyfixed
Need backtrace:                      https://bugs.php.net/fix.php?id=60534&r=needtrace
Need Reproduce Script:               https://bugs.php.net/fix.php?id=60534&r=needscript
Try newer version:                   https://bugs.php.net/fix.php?id=60534&r=oldversion
Not developer issue:                 https://bugs.php.net/fix.php?id=60534&r=support
Expected behavior:                   https://bugs.php.net/fix.php?id=60534&r=notwrong
Not enough info:                     https://bugs.php.net/fix.php?id=60534&r=notenoughinfo
Submitted twice:                     https://bugs.php.net/fix.php?id=60534&r=submittedtwice
register_globals:                    https://bugs.php.net/fix.php?id=60534&r=globals
PHP 4 support discontinued:          https://bugs.php.net/fix.php?id=60534&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=60534&r=dst
IIS Stability:                       https://bugs.php.net/fix.php?id=60534&r=isapi
Install GNU Sed:                     https://bugs.php.net/fix.php?id=60534&r=gnused
Floating point limitations:          https://bugs.php.net/fix.php?id=60534&r=float
No Zend Extensions:                  https://bugs.php.net/fix.php?id=60534&r=nozend
MySQL Configuration Error:           https://bugs.php.net/fix.php?id=60534&r=mysqlcfg



Thread (6 messages)

« previous php.bugs (#165886) next »