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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC