#37715 [Opn->Bgs]: array pointers resetting on copy

From: Date: Tue, 06 Jun 2006 15:32:39 +0000
Subject: #37715 [Opn->Bgs]: array pointers resetting on copy
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-98093@lists.php.net to get a copy of this message
ID: 37715 Updated by: tony2001@php.net Reported By: cmlburnett at gmail dot com -Status: Open +Status: Bogus Bug Type: Arrays related Operating System: Linux 2.6 PHP Version: 5.1.4 New Comment: >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. I hope you don't expect to be able to make a copy without reading the original data - this is when the pointer is changed. This is expected and not going to be changed. Previous Comments: ------------------------------------------------------------------------ [2006-06-06 15:27:02] cmlburnett at gmail dot com 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 this bug report at http://bugs.php.net/?id=37715&edit=1

« previous php.bugs (#98093) next »