note 46853 deleted from function.shuffle by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 Jul 2008 17:41:02 +0000 |
| Subject: | note 46853 deleted from function.shuffle by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142713@lists.php.net to get a copy of this message | ||
Note Submitter: Vladimir Kornea of typetango.com
----
shuffle() does not maintain key associations. This is how to shuffle an associative array without
losing key associations:
<?php
function shuffle_me($shuffle_me) {
$randomized_keys = array_rand($shuffle_me, count($shuffle_me));
foreach($randomized_keys as $current_key) {
$shuffled_me[$current_key] = $shuffle_me[$current_key];
}
return $shuffled_me;
}
?>
This is simpler than the other methods described here and it actually works.