php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62487 next/current and each behave differently with unset
Submitted: 2012-07-05 22:07 UTC Modified: 2012-07-06 10:12 UTC
From: justin at crowdfavorite dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.4 OS: OS X
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: justin at crowdfavorite dot com
New email:
PHP Version: OS:

 

 [2012-07-05 22:07 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-06 10:12 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2012-07-06 10:12 UTC] johannes@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

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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 08 23:01:32 2025 UTC