|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44063 RecursiveIteratorIterator needs rewind right after creation
Submitted: 2008-02-06 14:28 UTC Modified: 2008-02-07 09:32 UTC
From: smirnov at h-type dot com Assigned: helly (profile)
Status: Not a bug Package: SPL related
PHP Version: 5.2.5 OS: *
Private report: No CVE-ID: None
 [2008-02-06 14:28 UTC] smirnov at h-type dot com
RecursiveIteratorIterator returns one extra result if we don't provide rewind() before while.

Reproduce code:
$array = array( 
    array('name'=>'butch', 'sex'=>'male'), 
    array('name'=>'fido', 'sex'=>'male'), 

$it=new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); 

//$it->rewind(); //The result will be as expected if uncomment this
  echo $it->key().' -- '.$it->current()."\n"; 

Expected result:
name -- butch
sex -- male
name -- fido
sex -- male
name -- girly
sex -- female

Actual result:
0 -- Array
name -- butch
sex -- male
name -- fido
sex -- male
name -- girly
sex -- female


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-07 01:07 UTC]
Marcus, is my impression or is missing a rewind in this constructor?
 [2008-02-07 09:32 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Flipe, your assumption is wrong. Adding rewind to the constructor would result in a doubled call to rewind on the ArrayIterator.

Now the whole Iterator collection is designed to not duplicate any calls and to be used by foreach. So you are basically missing the rewind call, which by the way all examples with while have (at least my own ones).

I might however look into this again and check whether I can do something about the starting value in case of the missing rewind call on the ArrayIterator.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Jul 24 12:01:23 2021 UTC