note 80586 added to function.shuffle
| From: | rich at home dot nl | Date: | Wed, 23 Jan 2008 17:17:08 +0000 |
| Subject: | note 80586 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135340@lists.php.net to get a copy of this message | ||
Function that does the same as shuffle(), but preserves key=>values.
(Based on Vladimir's function)
<?php
function shuffle_assoc(&$array) {
if (count($array)>1) { //$keys needs to be an array, no need to shuffle 1 item anyway
$keys = array_rand($array, count($array));
foreach($keys as $key)
$new[$key] = $array[$key];
$array = $new;
}
return true; //because it's a wannabe shuffle(), which returns true
}
$test = array(1=>'one','two'=>'2',3=>'3');
print_r($test);
shuffle_assoc($test);
print_r($test);
?>
Array
(
[1] => one
[two] => 2
[3] => 3
)
Array
(
[two] => 2
[1] => one
[3] => 3
)
----
Server IP: 89.248.170.58
Probable Submitter: 82.75.107.10
----
Manual Page -- http://www.php.net/manual/en/function.shuffle.php
Edit -- https://master.php.net/note/edit/80586
Del: integrated -- https://master.php.net/note/delete/80586/integrated
Del: useless -- https://master.php.net/note/delete/80586/useless
Del: bad code -- https://master.php.net/note/delete/80586/bad+code
Del: spam -- https://master.php.net/note/delete/80586/spam
Del: non-english -- https://master.php.net/note/delete/80586/non-english
Del: in docs -- https://master.php.net/note/delete/80586/in+docs
Del: other reasons-- https://master.php.net/note/delete/80586
Reject -- https://master.php.net/note/reject/80586
Search -- https://master.php.net/manage/user-notes.php