php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33172 changing array during foreach iteration
Submitted: 2005-05-28 16:24 UTC Modified: 2005-06-06 10:15 UTC
From: tomas_matousek at hotmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.4 OS: WinXP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tomas_matousek at hotmail dot com
New email:
PHP Version: OS:

 

 [2005-05-28 16:24 UTC] 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)
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-05-28 16:30 UTC] 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-06-03 21:22 UTC] 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-06-03 23:39 UTC] tomas_matousek at hotmail dot com
Doesn't work either.
 [2005-06-06 10:15 UTC] sniper@php.net
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.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 15:01:33 2024 UTC