From: cmlburnett at gmail dot com
Operating system: Linux 2.6
PHP version: 5.1.4
PHP Bug Type: Arrays related
Bug description: array pointers resetting on copy
Description:
------------
If I have an array of arrays and I iterate over the parent array with a
foreach loop "foreach($a as $k => $d)" then the internal pointers of the
subarrays will be reset. I presume this is caused by copying $a[$k] into
$d on each iteration. It is also reproduced by simply doing an array copy
"$c = $a".
Ultimately, this is an issue of array copying.
Reproduce code:
---------------
$a = array(
'a' => array(
'A', 'B', 'C', 'D',
),
'b' => array(
'AA', 'BB', 'CC', 'DD',
),
);
// Set the pointer of $a to 'b' and the pointer of 'b' to 'CC'
reset($a);
next($a);
next($a['b']);
next($a['b']);
next($a['b']);
var_dump(key($a['b']));
foreach($a as $k => $d)
{
}
// Alternatively $c = $a; and foreachloop removal will cause identical
results.
var_dump(key($a['b']));
Expected result:
----------------
int(3)
int(3)
Having made no changes to $a I would expect nothing about $a to change.
As an end-user, if I'm making a *copy* of something then I would not
expect the original to change and this would include internal state.
Really: I would prefer the pointer not change in the original and that the
copy would retain the pointer as well.
Actual result:
--------------
int(3)
int(0)
--
Edit bug report at http://bugs.php.net/?id=37715&edit=1
--
Try a CVS snapshot (PHP 4.4):
http://bugs.php.net/fix.php?id=37715&r=trysnapshot44
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=37715&r=trysnapshot52
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=37715&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=37715&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=37715&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=37715&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=37715&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=37715&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=37715&r=support
Expected behavior: http://bugs.php.net/fix.php?id=37715&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=37715&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=37715&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=37715&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=37715&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=37715&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=37715&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=37715&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=37715&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=37715&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=37715&r=mysqlcfg