php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51942 ArrayIterator::rewind() doesn't rewind to first element, but to second.
Submitted: 2010-05-28 14:45 UTC Modified: 2010-05-28 14:55 UTC
From: develop at jacobkiers dot net Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.3.2 OS: Ubuntu 10.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: develop at jacobkiers dot net
New email:
PHP Version: OS:

 

 [2010-05-28 14:45 UTC] develop at jacobkiers dot net
Description:
------------
ArrayIterator::rewind() doesn't rewind to first element, but to second.

Test script:
---------------
<?php
$i = new ArrayIterator(array(1,2,3,4,5,6));

$r = false; # To prevent looping infinitely.

foreach($i as $j) {
        if ($j == 4 && $r == false) {
                $i->rewind();
                $r = true;
        }   
        echo $j.",";
}

Expected result:
----------------
1,2,3,4,1,2,3,4,5,6,

Actual result:
--------------
1,2,3,4,2,3,4,5,6,

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-28 14:55 UTC] salathe@php.net
-Status: Open +Status: Bogus
 [2010-05-28 14:55 UTC] salathe@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

This is expected behaviour due to the way that iterators work with foreach.  When 
$j is 4, the iterator gets rewound. At this point $j is still 4 which is then 
echoed.  Then when going on to the next iteration (where you expect to get $j = 1) 
the $i->next() method is called, moving to the second item.
 [2010-05-28 15:56 UTC] develop at jacobkiers dot net
Thanks for the quick reply. I'm sorry for wasting your time.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 01:01:35 2025 UTC