Voting

: seven plus two?
(Example: nine)

The Note You're Voting On

olav at fwt dot no
14 years ago
When unsetting elements as you go it will not remove the second index of the Array being worked on. Im not sure exactly why but there is some speculations that when calling unsetOffset(); it resets the pointer aswell.

<?php

$a
= new ArrayObject( range( 0,9 ) );
$b = new ArrayIterator( $a );

for (
$b->rewind(); $b->valid(); $b->next() )
{
echo
"#{$b->key()} - {$b->current()} - \r\n";
$b->offsetUnset( $b->key() );
}

?>

To avoid this bug you can call offsetUnset in the for loop

<?php
/*** ... ***/
for ( $b->rewind(); $b->valid(); $b->offsetUnset( $b->key() ) )
{
/*** ... ***/
?>

Or unset it directly in the ArrayObject
<?php
/*** ... ***/
$a->offsetUnset( $b->key() );
/*** ... ***/
?>

which will produce correct results

<< Back to user notes page

To Top