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 (profile)
Status: Not a bug Package: SPL related
PHP Version: 5.2.5 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
32 - 16 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 05:01:30 2024 UTC