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

  • Home
  • SEARCH
  • 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 7668787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:26:46+00:00 2026-05-31T15:26:46+00:00

I am using PHP 5.3.6 from MAMP . I have a use case where

  • 0

I am using PHP 5.3.6 from MAMP.

I have a use case where it would be best to use PHP’s Iterator interface methods, next(), current(), and valid() to iterate through a collection. A foreach loop will NOT work for me in my particular situation. A simplified while loop might look like

<?php
while ($iter->valid()) {
  // do something with $iter->current()
  $iter->next();
}

Should the above code always work when $iter implements PHP’s Iterator interface? How does PHP’s foreach keyword deal with Iterators?

The reason I ask is that the code I am writing may be given an ArrayIterator or a MongoCursor. Both implement PHP’s Iterator interface but they behave differently. I would like to know if there is a bug in PHP or PHP’s Mongo extension.

ArrayIterator::valid() returns true before any call to next() — immediately after the ArrayIterator is created.

MongoCursor::valid() only returns true after the first call to next(). Therefore the while loop above will never execute.

At risk of being verbose, the following code demonstrates these assertions:

<?php

// Set up array iterator
$arr = array("first");
$iter = new \ArrayIterator($arr);

// Test array iterator
echo(($iter->valid() ? "true" : "false")."\n"); // Echoes true
var_dump($iter->current()."\n");                // "first"
$iter->next();
echo(($iter->valid() ? "true" : "false")."\n"); // Echoes false


// Set up mongo iterator
$m = new \Mongo();
$collection = $m->selectDB("iterTest")->selectCollection("mystuff");
$collection->drop(); // Ensure collection is empty
$collection->insert(array('a' => 'b'));
$miter = $collection->find(); // should find one object

// Test mongo iterator
echo(($miter->valid() ? "true" : "false")."\n"); // Echoes false

$miter->next();

echo(($miter->valid() ? "true" : "false")."\n"); // Echoes true
var_dump($miter->current());                     // Array(...)

Which implementation is correct? I found little documentation to support either behavior, and the official PHP documentation is either ambiguous or I’m reading it wrong. The doc for Iterator::valid() states:

This method is called after Iterator::rewind() and Iterator::next() to check if the current position is valid.

This would suggest that my while loop should first call next().

Yet the PHP documentation for Iterator::next states:

This method is called after each foreach loop.

This would suggest that my while loop is correct as written.

To summarize – how should PHP iterators behave?

  • 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-05-31T15:26:47+00:00Added an answer on May 31, 2026 at 3:26 pm

    This is an interesting question. I’m not sure why a foreach won’t work for you, but I have some ideas.

    Take a look at the example given on the Iterator interface reference page. It shows the order in which PHP’s internal implementation of foreach calls the Iterator methods. In particular, notice that when the foreach is first set up, the very first call is to rewind(). This example, though it’s not well-annotated, is the basis for my answer.

    I’m not sure why a MongoCursor would not return true for valid() until after next() is called, but you should be able to reset either type of object by calling rewind() prior to your loop. So you would have:

    // $iter may be either MongoCursor or ArrayIterator
    
    $iter->rewind();
    while( $iter->valid() ){
        // do something with $iter->current()
        $iter->next();
    }
    

    I believe this should work for you. If it does not, the Mongo class may have a bug in it.

    Edit: Mike Purcell’s answer correctly calls out that ArrayIterator and Iterator are not the same. However, ArrayIterator implements Iterator, so you should be able to use rewind() as I show above on either of them.

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

Sidebar

Related Questions

i am using php 5.2.8 i have index.html, which loads LOAD.PHP from IFRAME. iframe
what is best way to send email from PHP using server authantication. Userid &
I have been using php from some time and I have noticed that it
Basically, I have populated a dropdown list using php from a database on a
We have an issue using the PEAR libraries on Windows from PHP . Pear
I have an application I've written in PHP from scratch. I'm using PHP's native
Possible Duplicate: save image from php url using php How can i use php
I'm trying to get away from using Mamp, so have recently installed mysql, edited
I am trying to call a COM object from PHP using the COM interop
I'm using PHP to copy JPGs from a remote server to my own server.

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.