php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42654 recursiveIteratorIterator modify only part of leaves
Submitted: 2007-09-13 09:48 UTC Modified: 2007-10-18 05:52 UTC
From: ltaupiac at lfdj dot com Assigned: helly (profile)
Status: Closed Package: SPL related
PHP Version: 5CVS-2007-09-13 (snap) OS: windows
Private report: No CVE-ID: None
 [2007-09-13 09:48 UTC] ltaupiac at lfdj dot com
Description:
------------
when recursively iterate over recursive array to alter leaves, only first leaves are altered.



Reproduce code:
---------------
$data = array ('key1' => 'val1', array('key2' => 'val2'), 'key3' => 'val3');

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
foreach($iterator as $foo) {
    $key = $iterator->key();
    switch($key) {
        case 'key1': // first level 
        case 'key2': // recursive level
            echo "update $key<br>";
            $iterator->offsetSet($key, 'alter');
    }
}
$copy = $iterator->getArrayCopy();
var_dump($copy);

Expected result:
----------------
update key1
update key2
array(3) {
  ["key1"]=>
  string(5) "alter"
  [0]=>
  array(1) {
    ["key2"]=>
    string(4) "alter"
  }
  ["key3"]=>
  string(4) "val3"
}

Actual result:
--------------
update key1
update key2
array(3) {
  ["key1"]=>
  string(5) "alter"
  [0]=>
  array(1) {
    ["key2"]=>
    string(4) "val2"
  }
  ["key3"]=>
  string(4) "val3"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-13 09:51 UTC] jani@php.net
Assigned to the SPL maintainer.
 [2007-10-18 05:52 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC