note 56338 added to function.shuffle

From: Date: Wed, 31 Aug 2005 05:55:05 +0000
Subject: note 56338 added to function.shuffle
Groups: php.notes 
Request: Send a blank email to php-notes+get-94478@lists.php.net to get a copy of this message
Here's a pretty little function to shuffle an associative array, taking a bit different approach from the earlier entries. <?PHP function ass_array_shuffle ($array) { while (count($array) > 0) { $val = array_rand($array); $new_arr[$val] = $array[$val]; unset($array[$val]); } return $new_arr; } ?> In the function above, the original array is randomly scavenged upon until nothing is left of it. This removes the need for any temporary variables, or indeed for any superfluous tossing around of stuff. ---- Manual Page -- http://www.php.net/manual/en/function.shuffle.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56338 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56338&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56338&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56338&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56338&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56338&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56338&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56338&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94478) next »