note 99059 added to function.shuffle
| From: | ezakto at ezakto dot com | Date: | Sun, 25 Jul 2010 06:13:38 +0000 |
| Subject: | note 99059 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171110@lists.php.net to get a copy of this message | ||
This is a replica of shuffle() but preserving keys (associative and non-associative)
bool kshuffle ( array &$array )
<?php
function kshuffle(&$array) {
if(!is_array($array) || empty($array)) {
return false;
}
$tmp = array();
foreach($array as $key => $value) {
$tmp[] = array('k' => $key, 'v' => $value);
}
shuffle($tmp);
$array = array();
foreach($tmp as $entry) {
$array[$entry['k']] = $entry['v'];
}
return true;
}
$array = array('first' => 0, 'second' => 1, 'third' => 2);
kshuffle($array);
print_r($array); // [second] => 1 [first] => 0 [third] => 2
$array = array('first', 'second', 'third');
kshuffle($array);
print_r($array); // [1] => second [2] => third [0] => first
?>
----
Server IP: 69.147.83.197
Probable Submitter: 190.226.143.7
----
Manual Page -- http://www.php.net/manual/en/function.shuffle.php
Edit -- https://master.php.net/note/edit/99059
Del: integrated -- https://master.php.net/note/delete/99059/integrated
Del: useless -- https://master.php.net/note/delete/99059/useless
Del: bad code -- https://master.php.net/note/delete/99059/bad+code
Del: spam -- https://master.php.net/note/delete/99059/spam
Del: non-english -- https://master.php.net/note/delete/99059/non-english
Del: in docs -- https://master.php.net/note/delete/99059/in+docs
Del: other reasons-- https://master.php.net/note/delete/99059
Reject -- https://master.php.net/note/reject/99059
Search -- https://master.php.net/manage/user-notes.php