Bug #51942 [Com]: ArrayIterator::rewind() doesn't rewind to first element, but to second.

From: Date: Fri, 28 May 2010 13:56:33 +0000
Subject: Bug #51942 [Com]: ArrayIterator::rewind() doesn't rewind to first element, but to second.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-150910@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=51942&edit=1

 ID:               51942
 Comment by:       develop at jacobkiers dot net
 Reported by:      develop at jacobkiers dot net
 Summary:          ArrayIterator::rewind() doesn't rewind to first
                   element, but to second.
 Status:           Bogus
 Type:             Bug
 Package:          SPL related
 Operating System: Ubuntu 10.04
 PHP Version:      5.3.2

 New Comment:

Thanks for the quick reply. I'm sorry for wasting your time.


Previous Comments:
------------------------------------------------------------------------
[2010-05-28 14:55:59] 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 14:45:11] 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,


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=51942&edit=1


Thread (3 messages)

« previous php.bugs (#150910) next »