Edit report at https://bugs.php.net/bug.php?id=62487&edit=1

 ID:                 62487
 Updated by:         johan...@php.net
 Reported by:        justin at crowdfavorite dot com
 Summary:            next/current and each behave differently with unset
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Arrays related
 Operating System:   OS X
 PHP Version:        5.4.4
 Block user comment: N
 Private report:     N

 New Comment:

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

If we would change the behavior to what you want the current() after the 
unset() would refer to the previous element. Which means that would be accessed 
two times.

As a little picture: Assume we have an array with 5 elements, current pointer 
is pointing to element 2:

   0 1 2 3 4
       ^

Now we call unset(), 2 goes away:

   0 1 3 4
       ^

Basically the current pointer doesn't move, but now you call next

   0 1 3 4
         ^

That's what you're seeing.


Previous Comments:
------------------------------------------------------------------------
[2012-07-05 22:07:27] justin at crowdfavorite dot com

Description:
------------
Removing the current array element using unset() while iterating through the 
array with next() and current() causes the array pointer to miss the next array 
element.  

Removing the /next/ array element with unset() while iterating through the 
array with each() does /not/ cause similar behavior - the array element 
following is correctly picked up at the next call to each().

It seems like the behavior with next()/current() is incorrect (but that it 
would be good for the behavior to be consistent regardless).

Test script:
---------------
example code: http://pastebin.com/5HMumA0A

Expected result:
----------------
12345 - expected
12345
1245 - expected
1245
12345 - expected
12345

Actual result:
--------------
12345 - expected
12345
1245 - expected
1245
12345 - expected
1245


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



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

Reply via email to