note 104789 added to arrayiterator.offsetunset

From: Date: Thu, 07 Jul 2011 11:12:30 +0000
Subject: note 104789 added to arrayiterator.offsetunset
Groups: php.notes 
Request: Send a blank email to php-notes+get-181311@lists.php.net to get a copy of this message
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 ---- Server IP: 69.147.83.197 Probable Submitter: 77.247.156.227 ---- Manual Page -- http://www.php.net/manual/en/arrayiterator.offsetunset.php Edit -- https://master.php.net/note/edit/104789 Del: integrated -- https://master.php.net/note/delete/104789/integrated Del: useless -- https://master.php.net/note/delete/104789/useless Del: bad code -- https://master.php.net/note/delete/104789/bad+code Del: spam -- https://master.php.net/note/delete/104789/spam Del: non-english -- https://master.php.net/note/delete/104789/non-english Del: in docs -- https://master.php.net/note/delete/104789/in+docs Del: other reasons-- https://master.php.net/note/delete/104789 Reject -- https://master.php.net/note/reject/104789 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#181311) next »