note 46853 modified in function.shuffle by aidan
| From: | aidan@php.net | Date: | Tue, 26 Oct 2004 00:36:21 +0000 |
| Subject: | note 46853 modified in function.shuffle by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79100@lists.php.net to get a copy of this message | ||
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.
--was--
shuffle() does not maintain key associations. This is how to shuffle an associative array without
losing key associations:
// $shuffle_me is the array
$randomized_keys = array_rand($shuffle_me, count($shuffle_me));
foreach($randomized_keys as $current_key)
$shuffled_me[$current_key] = $shuffle_me[$current_key];
// $shuffled_me is now a shuffled $shuffle_me
This is simpler than the other methods described here and it actually works.
http://php.net/manual/en/function.shuffle.php