#35485 [Bgs->Opn]: foreach is not working correctly

From: Date: Wed, 30 Nov 2005 12:19:05 +0000
Subject: #35485 [Bgs->Opn]: foreach is not working correctly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-89451@lists.php.net to get a copy of this message
 ID:               35485
 User updated by:  vma1 at abv dot bg
 Reported By:      vma1 at abv dot bg
-Status:           Bogus
+Status:           Open
 Bug Type:         Arrays related
 Operating System: Slackware 10.1, kernel 2.4.31
 PHP Version:      5CVS-2005-11-30 (snap)
 New Comment:

The array is not being modified.


Previous Comments:
------------------------------------------------------------------------

[2005-11-30 12:54:27] tony2001@php.net

Do not modify array in the foreach() loop.


------------------------------------------------------------------------

[2005-11-30 12:41:39] vma1 at abv dot bg

Description:
------------
When using foreach($arr as $index => &$item), the $item reference does
not always allow a modification of the referenced element.

Reproduce code:
---------------
<?
$items = array (array (1), array (1));
foreach ($items as $index => &$one_item) {
	printf ("C1 IDENTICAL: %u\n", $items [$index] === $one_item);
	each ($items [$index]);
	printf ("C2 IDENTICAL: %u\n", $items [$index] === $one_item);
	$one_item = 678;
	printf ("C3 IDENTICAL: %u\n", $items [$index] === $one_item);
}
?>


Expected result:
----------------
C1 IDENTICAL: 1
C2 IDENTICAL: 1
C3 IDENTICAL: 1
C1 IDENTICAL: 1
C2 IDENTICAL: 1
C3 IDENTICAL: 1


Actual result:
--------------
C1 IDENTICAL: 1
C2 IDENTICAL: 1
C3 IDENTICAL: 1
C1 IDENTICAL: 1
C2 IDENTICAL: 1
C3 IDENTICAL: 0



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=35485&edit=1


Thread (8 messages)

« previous php.bugs (#89451) next »