Copying of arrays

From: Date: Sat, 19 Apr 2003 02:22:09 +0000
Subject: Copying of arrays
Groups: php.internals 
Request: Send a blank email to internals+get-969@lists.php.net to get a copy of this message
Bug #23195 demonstrates an interesting in the way arrays are copied in PHP. If inside the code that depends on the position of the array the array is copied to another variable it's internal position gets reset. This, as the example below will show will cause an unterminated loop. <?php $arr = array('a', 'b'); while (each($arr)) $arr2 = $arr; ?> The source of the problem could be traced to zend_hash_copy(), which will always reset the internal pointer of the target array to the 'head' element. This can be remedied by setting the internal pointer of the new array to the same as the source array, which solves the problem (patch attached). Or we document this problem and tell people to assign values by reference if they need to assign an array, which is being traversed to another variable. Ilia

Index: Zend/zend_hash.c =================================================================== RCS file: /repository/Zend/zend_hash.c,v retrieving revision 1.87.4.2 diff -u -3 -p -r1.87.4.2 zend_hash.c --- Zend/zend_hash.c 31 Dec 2002 16:23:02 -0000 1.87.4.2 +++ Zend/zend_hash.c 19 Apr 2003 02:12:20 -0000 @@ -790,7 +790,7 @@ ZEND_API void zend_hash_copy(HashTable * } p = p->pListNext; } - target->pInternalPointer = target->pListHead; + target->pInternalPointer = source->pInternalPointer; }
« previous php.internals (#969) next »