php.net |  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
Status: Not a bug Package: SPL related
PHP Version: 5.2.5 OS: *
Private report: No CVE-ID:
 [2008-02-06 14:28 UTC] smirnov at h-type dot com
Description:
------------
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'), 
    array('name'=>'girly','sex'=>'female') 
);

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

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

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-07 01:07 UTC] felipe@php.net
Marcus, is my impression or is missing a rewind in this constructor?

http://felipe.ath.cx/diff/bug44063.diff
 [2008-02-07 09:32 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

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-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 20:01:57 2014 UTC