#49347 [NEW]: iterating through an array plus creating a refererence to the current element
| From: | zoli at lippai dot net | Date: | Mon, 24 Aug 2009 15:44:06 +0000 |
| Subject: | #49347 [NEW]: iterating through an array plus creating a refererence to the current element | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-141437@lists.php.net to get a copy of this message | ||
From: zoli at lippai dot net
Operating system: OS X 10.5.8
PHP version: 5.3.0
PHP Bug Type: Arrays related
Bug description: iterating through an array plus creating a refererence to the current element
Description:
------------
When I iterate through an array with "for" and create a reference to the
current element inside the for, then the last element of the array will be
a reference to the previous element.
If I unset the reference variable at the end of every cycle, then it works
fine.
Reproduce code:
---------------
<?php
$list = array(
array('id' => 1),
array('id' => 2)
);
for($i = 0; $i < count($list); $i++) {
$item = &$list[$i];
$item['someValue'] = 'x';
}
foreach($list as $item)
{
var_dump($item);
}
Expected result:
----------------
array(2) {
["id"]=>
int(1)
["someValue"]=>
string(1) "x"
}
array(2) {
["id"]=>
int(2)
["someValue"]=>
string(1) "x"
}
Actual result:
--------------
array(2) {
["id"]=>
int(1)
["someValue"]=>
string(1) "x"
}
array(2) {
["id"]=>
int(1)
["someValue"]=>
string(1) "x"
}
(Please note the second elements id, which is 1 instead of 2)
--
Edit bug report at http://bugs.php.net/?id=49347&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49347&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49347&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49347&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=49347&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49347&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=49347&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=49347&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=49347&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=49347&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=49347&r=support
Expected behavior: http://bugs.php.net/fix.php?id=49347&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=49347&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=49347&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=49347&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49347&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=49347&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=49347&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=49347&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=49347&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=49347&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=49347&r=mysqlcfg