Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:49:23+00:00 2026-06-14T21:49:23+00:00

RecursiveIteratorIterator returns extra result if rewind() is not called before while loop Example $array

  • 0

RecursiveIteratorIterator returns extra result if rewind() is not called before while loop

Example

$array = array("A","B","C");
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
//$iterator->rewind() ; this would fix it 
while ( $iterator->valid() ) {
    print($iterator->current()) ;
    $iterator->next();
}

Output

AABC  <--- Instead of ABC
  • Why an extra A not C ?
  • The Array has never been initiated or called why is $iterator->rewind() required for while loop
  • foreach works perfectly without having to call rewind whats the differences between foreach and while when working with iterators

Code In action

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:49:24+00:00Added an answer on June 14, 2026 at 9:49 pm

    I’m going to answer the questions in reverse order:

    foreach works perfectly without having to call rewind whats the differences between those foreach and while when working with iterators

    foreach internally does a call to rewind(), that’s why you don’t have to do it yourself. This is always done, so even if you have already used an iterator, the foreach loop will start over from the beginning. (You can avoid this by wrapping it in a NoRewindIterator).

    The Array has never been initiated or called why is $iterator->rewind() required for while loop

    The SPL iterators are designed to be used with foreach and to avoid duplicate method calls in this case. If the RecursiveIteratorIterator would call the RecursiveArrayIterator::rewind() method on construction, then it would be called again when the foreach loop starts. That’s why the call isn’t done.

    Why an extra A not C ?

    To figure this out it is nice to see which methods of the RecursiveArrayIterator actually get called:

    <?php
    
    class DebugRAI extends RecursiveArrayIterator {
        public function rewind() { echo __METHOD__, "\n"; return parent::rewind(); }
        public function current() { echo __METHOD__, "\n"; return parent::current(); }
        public function key() { echo __METHOD__, "\n"; return parent::key(); }
        public function valid() { echo __METHOD__, "\n"; return parent::valid(); }
        public function next() { echo __METHOD__, "\n"; return parent::next(); }
    }
    
    $array = array("A", "B", "C");
    $iterator = new RecursiveIteratorIterator(new DebugRAI($array));
    while ($iterator->valid()) {
        echo $iterator->current(), "\n";
        $iterator->next();
    }
    

    This produces the following output:

    DebugRAI::valid
    DebugRAI::current
    A
    DebugRAI::valid
    DebugRAI::valid
    A
    DebugRAI::next
    DebugRAI::valid
    DebugRAI::valid
    DebugRAI::current
    B
    DebugRAI::next
    DebugRAI::valid
    DebugRAI::valid
    DebugRAI::current
    C
    DebugRAI::next
    DebugRAI::valid
    DebugRAI::valid
    

    The output looks a bit odd, in particular the second iteration misses the next() call, so it just stays at the same element.

    The reason for this is a peculiarity in the RecursiveIteratorIterator implementation: Iterators start off in the RS_START state and the first next call in this state only checks hasChildren(), but does not actually call the underlying iterator’s next() method. After this was done it switches into the RS_NEXT mode in which the next() call happens properly. That’s why the forward move is delayed by one step.

    In my eyes this is a bug, but https://bugs.php.net/bug.php?id=44063 claims otherwise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ZipArchive: function zip_dir($source, $target){ $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST); $zip
I have created a custom iterator that extends RecursiveIteratorIterator which I use to iterate
Let's say I have following ArrayObject : $array_object = new ArrayObject(array( array('1', '2', '3',
Example: class MyRecursiveIteratorIterator extends RecursiveIteratorIterator{ public function current(){ echo 'START'; return parent::current(); } }
I am using the for each below with objective $hierachy = new hierachy; $iterator
Possible Duplicate: PHP SPL RecursiveDirectoryIterator RecursiveIteratorIterator retrieving the full tree I am not sure
My function cleanup looks like that. function cleanUp($exdirs, $exfiles){ $it = new RecursiveIteratorIterator( new
Here's my php script: <?php $path = $_SERVER['DOCUMENT_ROOT'].'/test'; $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
I am new to PHP and not very clear how to parse JSON with
i'm using RecursiveIteratorIterator() to explore subfolders of my current path, but in this way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.