|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63154 SplDoublyLinkedList should handle list modifications from outside of the object
Submitted: 2012-09-24 18:44 UTC Modified: 2014-12-30 10:41 UTC
From: maciej dot sz at gmail dot com Assigned: levim (profile)
Status: No Feedback Package: SPL related
PHP Version: * OS: Irrelevant
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: maciej dot sz at gmail dot com
New email:
PHP Version: OS:


 [2012-09-24 18:44 UTC] maciej dot sz at gmail dot com
When I unset more then one element while iterating through SplDoublyLinkedList object it throws an OutOfRangeException exception. Im guessing that this is because each time an the offsetUnset() method is called, the keys of the list are reset. This should not be happening until the loop is rewind()'ed.

Test script:
$List = new SplDoublyLinkedList();


foreach ( $List as $key => $value ) {
    echo "Current element: key = {$key}, value = '{$value}'. ";
    if ( in_array($value, ['b', 'd']) ) {
        echo "MATCH! performing unset";
    echo "\n";

Expected result:
The elements should be removed from the list, and no exception should be thrown. At the very least it should act as the ArrayObject object: triggers a notice, but gets the job done. But the perfect solution would be the way ArrayIterator does it - clean, no errors:

$Arr = new ArrayIterator(['a', 'b', 'c', 'd']);
foreach ( $Arr as $key => $value ) {
    echo "Current element: key = {$key}, value = '{$value}'. ";
    if ( in_array($value, ['b', 'd']) ) {
        echo "MATCH! performing unset";
    echo "\n";

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-09 02:21 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: levim
 [2012-12-09 02:21 UTC]
This is not a bug.  However, the expected work-around would be:

$list = new SplDoublyLinkedList();
$list[] = 0;
$list[] = 1;
$list[] = 2;
$list[] = 3;

for ($list->rewind(); $list->valid(); ) {
    if (in_array($list->current(), array(0, 2))) {
    } else {

However, this does not work. I am not entirely sure which function/functions 
is/are faulty here.
 [2014-04-17 14:59 UTC]
-Status: Assigned +Status: Feedback -Type: Bug +Type: Feature/Change Request -Operating System: irrelevant +Operating System: Irrelevant -PHP Version: 5.4.7 +PHP Version: *
 [2014-04-17 14:59 UTC]
I spent a bit of time figuring out why my work-around doesn't work. The reason is when you remove an element the remaining keys are renumbered. This means that there is no easy way to get this to work as expected. If we were to change SplDoublyLinkedList to not reorder the keys (which would make it behave like the ArrayIterator example) it would be a major BC break. I don't think we can do it. What are your thoughts?
 [2014-07-30 22:37 UTC] sun at unleashedmind dot com
I just ran into

Wondering whether this bug here + the fact that keys are re-keyed upon unset, and the SplDoublyLinkedList::IT_MODE_LIFO bug outlined in the gist, are effectively the same bug, or whether I should file a separate issue?
 [2014-12-30 10:41 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Aug 10 05:05:46 2022 UTC