|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2006-06-06 15:32 UTC] tony2001@php.net
[2007-07-24 18:29 UTC] dmitry@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 23 16:00:01 2025 UTC |
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)