Bug #55157 [Com]: ArrayIterator::offsetUnset(); does not work correctly

From: Date: Sun, 19 Apr 2015 00:47:37 +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-192194@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:         mw dot wanrooij at vodafonevast dot nl
 Reported by:        olav at fwt dot no
 Summary:            ArrayIterator::offsetUnset(); does not work
                     correctly
 Status:             Assigned
 Type:               Bug
 Package:            SPL related
 Operating System:   openSUSE 11.4/Debian 5
 PHP Version:        Irrelevant
 Assigned To:        colder
 Block user comment: N
 Private report:     N

 New Comment:

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?


Previous Comments:
------------------------------------------------------------------------
[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).

------------------------------------------------------------------------
[2011-07-07 07:18:36] joey@php.net

Sorry, I should clarify that the code above gives:

array(1) { [1]=> int(1) }

Whereas you'd probably expect:

array(0) { }

------------------------------------------------------------------------


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


Thread (15 messages)

« previous php.bugs (#192194) next »