note 95010 added to function.next

From: Date: Tue, 08 Dec 2009 21:19:44 +0000
Subject: note 95010 added to function.next
Groups: php.notes 
Request: Send a blank email to php-notes+get-163720@lists.php.net to get a copy of this message
PHP: 5.2.10-2ubuntu6.3 (default apt-get installation on actual, see Date, jaunty 9.10 Ubuntu Distro - G33kWoRDs) Have a look at your array pointer if you copy an array - the pointer will be copied, too. For example if you got this construct: <?php $array = array('zero','one','two','three','four','five','six','seven'); $array2 = $array; next($array); echo key($array); echo key($array2); // will output: // 1 // 0 ?> But if you copy the array after you've setted the pointer, the pointer will be copied, too: <?php $array = array('zero','one','two','three','four','five','six','seven'); next($array); $array2 = $array; echo key($array); echo key($array2); // will output: // 1 // 1 ?> What's more is, that foreach not resetting the pointer after walk through: <?php $array = array('zero','one','two','three','four','five','six','seven'); next($array); $array2 = array(); foreach($array AS $key => $value){ echo $key; $array2[$key] = $value; } echo var_dump(key($array)); echo key($array2); // will output for foreach: // 0 1 2 3 4 5 6 7 // and for the keys // NULL // 0 ?> The php-functions seems to reset the pointer on the given position after walk through (i don't know the internal handling - there could be used a copy of the array, too): <?php $array = array('zero','one','two','three','four','five','six','seven'); next($array); $array2 = array_values($array); echo key($array); echo key($array2); // will output: // 1 // 0 ?> There are a lot Methods like array_merge($array) that will neither reset the pointer of $array nor copy the pointer to $array2. Have a look on this. I Hope this was a little helpfull. ---- Server IP: 69.147.83.197 Probable Submitter: 77.21.110.15 ---- Manual Page -- http://www.php.net/manual/en/function.next.php Edit -- https://master.php.net/note/edit/95010 Del: integrated -- https://master.php.net/note/delete/95010/integrated Del: useless -- https://master.php.net/note/delete/95010/useless Del: bad code -- https://master.php.net/note/delete/95010/bad+code Del: spam -- https://master.php.net/note/delete/95010/spam Del: non-english -- https://master.php.net/note/delete/95010/non-english Del: in docs -- https://master.php.net/note/delete/95010/in+docs Del: other reasons-- https://master.php.net/note/delete/95010 Reject -- https://master.php.net/note/reject/95010 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163720) next »