note 119249 added to arrayiterator.seek

From: Date: Wed, 27 Apr 2016 02:20:14 +0000
Subject: note 119249 added to arrayiterator.seek
Groups: php.notes 
Request: Send a blank email to php-notes+get-205346@lists.php.net to get a copy of this message
<?php //seek alter the iterator's internal indice instead of the value that key() returns. //This is a big trap if combining with uasort/natsort function. $a = new ArrayObject([4,3,2,1]); $it = $a->getIterator(); $it->natsort(); //The Iterator preserve the key while sorting the array $it->rewind(); $first = $it->key(); //The first element is 1 and it's key is 3 echo $first. PHP_EOL; // 3 $it->next(); $second = $it->key(); echo $second. PHP_EOL; //2 $it->next(); $it->seek($first); //Was intended to seek to element 1, key 3, indice 0 echo $it->key() . PHP_EOL; //end up 0 because seek took parameter as indice instead of key. It seek to element indice 3, element 4 key 0. var_dump($it); /* Output: 3 2 0 object(ArrayIterator)#2 (1) { ["storage":"ArrayIterator":private]=> object(ArrayObject)#1 (1) { ["storage":"ArrayObject":private]=> array(4) { [3]=> int(1) [2]=> int(2) [1]=> int(3) [0]=> int(4) } } } */ ---- Server IP: 72.52.91.14 Probable Submitter: 101.187.188.58 ---- Manual Page -- http://php.net/manual/en/arrayiterator.seek.php Edit -- https://master.php.net/note/edit/119249 Del: integrated -- https://master.php.net/note/delete/119249/integrated Del: useless -- https://master.php.net/note/delete/119249/useless Del: bad code -- https://master.php.net/note/delete/119249/bad+code Del: spam -- https://master.php.net/note/delete/119249/spam Del: non-english -- https://master.php.net/note/delete/119249/non-english Del: in docs -- https://master.php.net/note/delete/119249/in+docs Del: other reasons-- https://master.php.net/note/delete/119249 Reject -- https://master.php.net/note/reject/119249 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205346) next »