note 56338 deleted from function.shuffle by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 14:53:56 +0000 |
| Subject: | note 56338 deleted from function.shuffle by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197210@lists.php.net to get a copy of this message | ||
Note Submitter: Markus of CodeWallah.Com
----
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.