|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76765 iterator_apply doesn't work with current() on DoublyLinkedLists
Submitted: 2018-08-19 20:00 UTC Modified: 2018-08-19 21:44 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: tezcatl at fedoraproject dot org Assigned:
Status: Verified Package: SPL related
PHP Version: Irrelevant OS: Linux
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.
Block user comment
Status: Assign to:
Bug Type:
From: tezcatl at fedoraproject dot org
New email:
PHP Version: OS:


 [2018-08-19 20:00 UTC] tezcatl at fedoraproject dot org
From manual page:
Iteration doesn't work as expected on SplDoublyLinkedList, as if next() was not called while using current() inside the callback passed to iterator_apply

Test script:
$ll = new SplDoublyLinkedList();



$iterations_done = iterator_apply($ll, function(Iterator $it) {
    echo $it->key(),"\t->", $it->current(),"\t->", ucfirst($it->current()),".\n";
    return true;
}, array($ll));
echo "Did iterate {$iterations_done} times \n";

foreach($ll as $key => $value) {
    $uppercase = ucfirst($value);
    echo "{$key}\t->{$value}\t->{$uppercase}.\n";

Expected result:
0	->one	->One.
1	->two	->Two.
2	->three	->Three.
3	->four	->Four.
Did iterate 4 times 
0	->one	->One.
1	->two	->Two.
2	->three	->Three.
3	->four	->Four.

Actual result:
0	->one	->One.
0	->one	->One.
0	->one	->One.
0	->one	->One.
Did iterate 4 times 
0	->one	->One.
1	->two	->Two.
2	->three	->Three.
3	->four	->Four.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-19 21:44 UTC]
-Status: Open +Status: Verified
 [2018-08-19 21:44 UTC]
Automatic iteration (foreach, iterator_apply()) on an
SplDoublyLinkedList happens on an external iterator, but
::current() and friends always work on the list directly. Removing
the ::rewind() call, which shouldn't be necessary for
iterator_apply() anyway, yields:

    0       ->      ->.
    0       ->      ->.
    0       ->      ->.
    0       ->      ->.
    Did iterate 4 times

Calling additionally ::next() in the callback brings the desired

[1] <>
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 22 03:01:32 2024 UTC