Bug #55157 [Asn->Opn]: ArrayIterator::offsetUnset(); does not work correctly

From: Date: Tue, 24 Oct 2017 07:25:42 +0000
Subject: Bug #55157 [Asn->Opn]: ArrayIterator::offsetUnset(); does not work correctly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212067@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 Updated by: kalle@php.net Reported by: olav at fwt dot no Summary: ArrayIterator::offsetUnset(); does not work correctly -Status: Assigned +Status: Open Type: Bug Package: SPL related Operating System: openSUSE 11.4/Debian 5 PHP Version: Irrelevant -Assigned To: colder +Assigned To: Block user comment: N Private report: N 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

« previous php.bugs (#212067) next »