note 45916 added to function.shuffle
| From: | BjornATsmokingmediaDOTcom at osu1 dot php dot net | Date: | Thu, 23 Sep 2004 12:02:35 +0000 |
| Subject: | note 45916 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-77242@lists.php.net to get a copy of this message | ||
To shuffle an associated array and remain the association between keys and values, use this
function:
<?php
$test_array = array("first" => 1,"second" => 2,"third" =>
3,"fourth" => 4);
function shuffle_assoc($input_array){
foreach($input_array as $key => $value){
$temp_array[$value][key] = $key;
}
shuffle($input_array);
foreach($input_array as $key => $value){
$return_array[$temp_array[$value][key]] = $value;
}
return $return_array;
}
echo "<pre>";
print_r(shuffle_assoc($test_array));
echo "</pre><hr />";
// this will output something like this:
/*
Array
(
[fourth] => 4
[second] => 2
[third] => 3
[first] => 1
)
*/
?>
----
Manual Page -- http://www.php.net/manual/en/function.shuffle.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45916
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45916&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45916&report=yes
Search -- http://master.php.net/manage/user-notes.php