Bug #63154 [Opn->Asn]: SplDoublyLinkedList should handle list modifications from outside of the object
Edit report at https://bugs.php.net/bug.php?id=63154&edit=1
ID: 63154
Updated by: levim@php.net
Reported by: maciej dot sz at gmail dot com
Summary: SplDoublyLinkedList should handle list modifications
from outside of the object
-Status: Open
+Status: Assigned
Type: Bug
Package: SPL related
Operating System: irrelevant
PHP Version: 5.4.7
-Assigned To:
+Assigned To: levim
Block user comment: N
Private report: N
New Comment:
This is not a bug. However, the expected work-around would be:
<?php
$list = new SplDoublyLinkedList();
$list[] = 0;
$list[] = 1;
$list[] = 2;
$list[] = 3;
for ($list->rewind(); $list->valid(); ) {
if (in_array($list->current(), array(0, 2))) {
$list->offsetUnset($list->key());
} else {
$list->next();
}
}
?>
However, this does not work. I am not entirely sure which function/functions
is/are faulty here.
Previous Comments:
------------------------------------------------------------------------
[2012-09-24 18:44:49] maciej dot sz at gmail dot com
Description:
------------
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();
$List->push('a');
$List->push('b');
$List->push('c');
$List->push('d');
foreach ( $List as $key => $value ) {
echo "Current element: key = {$key}, value = '{$value}'. ";
if ( in_array($value, ['b', 'd']) ) {
echo "MATCH! performing unset";
unset($List[$key]);
}
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";
unset($Arr[$key]);
}
echo "\n";
}
var_dump($Arr->getArrayCopy());
Actual result:
--------------
OutOfRangeException
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63154&edit=1
Thread (5 messages)