#33172 [Opn->Bgs]: changing array during foreach iteration
ID: 33172
Updated by: sniper@php.net
Reported By: tomas_matousek at hotmail dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: WinXP
PHP Version: 5.0.4
New Comment:
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
It works just like said in the manual.
Previous Comments:
------------------------------------------------------------------------
[2005-06-03 23:39:10] tomas_matousek at hotmail dot com
Doesn't work either.
------------------------------------------------------------------------
[2005-06-03 21:22:11] sniper@php.net
Please try using this CVS snapshot:
http://snaps.php.net/php5-STABLE-latest.tar.gz
For Windows:
http://snaps.php.net/win32/php5.0-win32-latest.zip
------------------------------------------------------------------------
[2005-05-28 16:30:17] tomas_matousek at hotmail dot com
Well, I've swapped the expected and the actual results.
I would expect all values of the array to be modified.
------------------------------------------------------------------------
[2005-05-28 16:24:41] tomas_matousek at hotmail dot com
Description:
------------
The behavior of foreach statment with reference value is not imho right
if the array gets modified during iteration.
See the code I've pasted here. If the array is written to (either by
unset or by a write operation) during iteration, the effect of & is
canceled, i.e. the values are not modified since the write operation.
This seems to me as bug because it is inconsistent with the
"definition" of foreach which should say that
foreach($a as $k => $v) {}
is (more or less) equivalent to
$copy_of_a = $a;
while(next($copy_of_a))
{
$k = key($copy_of_a);
$v = $copy_of_a[$k];
}
With the &, one can deduce the following "definition":
foreach($a as $k =>& $v) { }
is equivalent to
$copy_of_a = $a;
while(next($copy_of_a))
{
$k = key($copy_of_a);
$v =& $a[$k];
}
Using =& operator, a new value should be added if it has been unset in
the original array.
Reproduce code:
---------------
$a = array(0,1,2,3,4,5,6,7,8);
$i = 0;
foreach ($a as $k =>& $v)
{
$v+=100;
if ($i++==2)
{
unset($a[5]);
}
}
var_dump($a);
Expected result:
----------------
array(8) {
[0]=>
int(100)
[1]=>
int(101)
[2]=>
int(102)
[3]=>
int(3) // modification of values stops here
[4]=>
int(4)
[6]=>
int(6)
[7]=>
int(7)
[8]=>
int(8)
}
Actual result:
--------------
array(8) {
[0]=>
int(100)
[1]=>
int(101)
[2]=>
int(102)
[3]=>
int(103)
[4]=>
int(104)
[6]=>
int(106)
[7]=>
int(107)
[8]=>
int(108)
[5]=> // note: the new value should be added here
int(100)
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=33172&edit=1
Thread (5 messages)