Bug #62233 [Opn->Dup]: foreach destroys arrays
| From: | nikic@php.net | Date: | Tue, 05 Jun 2012 22:43:51 +0000 |
| Subject: | Bug #62233 [Opn->Dup]: foreach destroys arrays | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-171053@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62233&edit=1
ID: 62233
Updated by: nikic@php.net
Reported by: a1283465 at jnxjn dot com
Summary: foreach destroys arrays
-Status: Open
+Status: Duplicate
Type: Bug
Package: Arrays related
Operating System: Linux
PHP Version: 5.3.13
Block user comment: N
Private report: N
New Comment:
Duplicate of https://bugs.php.net/bug.php?id=60024 and a few
other bugs linked in there.
Previous Comments:
------------------------------------------------------------------------
[2012-06-05 15:59:00] a1283465 at jnxjn dot com
Description:
------------
See script outputs below.
Please note that the array is already broken after the first foreach run because the last array
element is now a pointer.
Workaround for the second foreach loop:
Do a unset($val) between the two foreach loops.
Test script:
---------------
$a = array(1,2,3);
var_dump($a); // array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
foreach ($a as &$val) { } // Now the array is broken
var_dump($a); // array(3) { [0]=> int(1) [1]=> int(2) [2]=> &int(3) }
foreach ($a as $val) { } // Now the array is destroyed
var_dump($a); // array(3) { [0]=> int(1) [1]=> int(2) [2]=> &int(2) }
echo $a[2]; // Output: 2, Expected: 3
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62233&edit=1