note 46853 added to function.shuffle
| From: | VladimirKorneaoftypetango dot com at osu1 dot php dot net | Date: | Mon, 25 Oct 2004 21:06:22 +0000 |
| Subject: | note 46853 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79092@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:
// $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.
----
Manual Page -- http://www.php.net/manual/en/function.shuffle.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46853
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46853&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46853&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46853&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46853&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46853&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46853&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46853&report=yes
Search -- http://master.php.net/manage/user-notes.php