Bug #55157 [Com]: ArrayIterator::offsetUnset(); does not work correctly
| From: | alexinbeijing at gmail dot com | Date: | Wed, 22 Apr 2020 07:24:45 +0000 |
| Subject: | Bug #55157 [Com]: ArrayIterator::offsetUnset(); does not work correctly | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226712@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55157&edit=1
ID: 55157
Comment by: alexinbeijing at gmail dot com
Reported by: olav at fwt dot no
Summary: ArrayIterator::offsetUnset(); does not work
correctly
Status: Open
Type: Bug
Package: SPL related
Operating System: openSUSE 11.4/Debian 5
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
This bug is already fixed and the report should be closed.
The original repro code prints keys 0, 2, 4, 6, and 8, which is expected. This is because when
::offsetUnset() deletes the current iteration key, it bumps the current iteration position to the
next element. Then, when ::next() is called, it bumps the position forward again. So it results in
iterating over every second element.
The subsequent repro code which was stated to only leave key 1 in place actually leaves all the
odd-numbered keys (1, 3, 5, 7, and 9) in place, for the same reason stated above.
Just seeing if documentation for ::offsetUnset() can be updated to make this clearer...
Previous Comments:
------------------------------------------------------------------------
[2015-04-19 00:47:35] mw dot wanrooij at vodafonevast dot nl
Same problem here, as mentioned earlier: "It seems that calling
ArrayIterator::offsetUnset moves the internal pointer to
the next element when removing the first index"
Is there any progress on a fix?
------------------------------------------------------------------------
[2014-08-29 06:46:30] jan dot kahoun at heureka dot cz
I have the same problem, are you planning to fix it?
------------------------------------------------------------------------
[2012-11-19 01:17:47] levim@php.net
It seems that calling ArrayIterator::offsetUnset moves the internal pointer to
the next element when removing the first index:
$a = range( 0,3 );
$b = new ArrayIterator( $a );
for ($b->rewind(); $b->valid(); ) {
echo "{$b->key()} => {$b->current()}\n";
$b->offsetUnset($b->key());
}
var_dump($b->getArrayCopy());
------------------------------------------------------------------------
[2011-07-19 15:18:57] gergo at gergoerdosi dot com
Please ignore my comment above. The offsetUnset() method unsets elements using
their index. Changing the code to this gives the expected result (0, 4):
$items = new ArrayObject(array(0, 1, 2, 3, 4));
foreach($items->getArrayCopy() as $item) {
if(in_array($item, array(1, 2, 3))) {
$items->offsetUnset($item);
}
}
var_dump($items->getArrayCopy());
------------------------------------------------------------------------
[2011-07-19 15:09:34] gergo at gergoerdosi dot com
I have the same problem, used this code:
$items = new ArrayObject(array(1, 2, 3, 4, 5));
foreach($items as $item) {
if(in_array($item, array(2, 3, 4))) {
$items->offsetUnset($item);
}
}
var_dump($items->getArrayCopy());
Expected result: array(1, 5), actual result: array(1, 3, 5).
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=55157
--
Edit this bug report at https://bugs.php.net/bug.php?id=55157&edit=1