note 112096 added to arrayiterator.append

From: Date: Thu, 02 May 2013 12:49:24 +0000
Subject: note 112096 added to arrayiterator.append
Groups: php.notes 
Request: Send a blank email to php-notes+get-194688@lists.php.net to get a copy of this message
As ArrayIterator is not a real list the implementation of "append" is a little bit confusing me. When using "append" I expected the new value at the postion "last element index + 1". This will not happen when you unset elements before. It seems like indexes once they're used are blacklisted end never used again, even if they're unset. Like suggested in http://www.php.net/manual/de/arrayiterator.offsetset.php#106775 ArrayIterator::append uses ArrayIterator::offsetSet with empty index parameter. So I've this two workarounds to get "append" to work like I want: <?php class myArrayIterator extends ArrayIterator { public function offsetSet($offset, $value) { if (is_null($offset)) { // offset == null when it's called by ArrayIterator::append $offset = $this->generateOffset(); // do it in a separate method } parent::offsetSet($offset, $value); // call the native implementation with an index $this->ksort(); // sort it to avoid confusion when it gets dumped or iterated } protected function generateOffset() { $offset = count($this); // take count as offset as it should be lastKey+1 while ($this->offsetExists($offset)) { // is it really empty? $offset++; // try the next one until there's an empty one } return $offset; } } class mySaveArrayIterator extends myArrayIterator { protected function generateOffset() { $offset = 0; // expect zero is the first possible key while ($this->offsetExists($offset)) { // try every key until there's an empty one $offset++; } return $offset; } } $data = array('foo', 'bar', 'baz'); $array = new ArrayIterator($data); $myArray = new myArrayIterator($data); $mySaveArray = new mySaveArrayIterator($data); // remove the last element $array ->offsetUnset(2); $myArray ->offsetUnset(2); $mySaveArray->offsetUnset(2); // append an element $array ->append('foobar'); $myArray ->append('foobar'); $mySaveArray->append('foobar'); // check the position of the new element print_r($array); print_r($myArray); print_r($mySaveArray); // remove some element $array ->offsetUnset(1); $myArray ->offsetUnset(1); $mySaveArray->offsetUnset(1); // again append an element $array ->append('foobarbaz'); $myArray ->append('foobarbaz'); $mySaveArray->append('foobarbaz'); // check the position of the new element print_r($array); print_r($myArray); print_r($mySaveArray); ?> Output: ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => foo [1] => bar [3] => foobar ) ) myArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => foo [1] => bar [2] => foobar ) ) mySaveArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => foo [1] => bar [2] => foobar ) ) ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => foo [3] => foobar [4] => foobarbaz ) ) myArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => foo [2] => foobar [3] => foobarbaz ) ) mySaveArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => foo [1] => foobarbaz [2] => foobar ) ) This helped me to treat the ArrayIterator as a list with valid indexes in a serial manner. ---- Server IP: 69.147.83.199 Probable Submitter: 141.65.129.219 ---- Manual Page -- http://php.net/manual/en/arrayiterator.append.php Edit -- https://master.php.net/note/edit/112096 Del: integrated -- https://master.php.net/note/delete/112096/integrated Del: useless -- https://master.php.net/note/delete/112096/useless Del: bad code -- https://master.php.net/note/delete/112096/bad+code Del: spam -- https://master.php.net/note/delete/112096/spam Del: non-english -- https://master.php.net/note/delete/112096/non-english Del: in docs -- https://master.php.net/note/delete/112096/in+docs Del: other reasons-- https://master.php.net/note/delete/112096 Reject -- https://master.php.net/note/reject/112096 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194688) next »