note 80004 added to function.shuffle
| From: | carrox at mail dot ru | Date: | Sun, 23 Dec 2007 22:15:30 +0000 |
| Subject: | note 80004 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133905@lists.php.net to get a copy of this message | ||
Very fast way to shuffle associative array and keep key=>value
<?
$tmp=array("one"=>1,"two"=>2,"three"=>3,"four"=>4);
$new=array();
$c=count($tmp);
$k=array_keys($tmp);
$v=array_values($tmp);
while($c>0) {
$i=array_rand($k);
$new[$k[$i]]=$v[$i];
unset($k[$i]); #exlude selected number from list
$c--;
}
print_r($tmp);
print_r($new);
?>
Result:
Array
(
[one] => 1
[two] => 2
[three] => 3
[four] => 4
)
Array
(
[two] => 2
[three] => 3
[four] => 4
[one] => 1
)
And thats all, folks!
----
Server IP: 195.54.192.44
Probable Submitter: 89.111.143.73
----
Manual Page -- http://www.php.net/manual/en/function.shuffle.php
Edit -- https://master.php.net/note/edit/80004
Del: integrated -- https://master.php.net/note/delete/80004/integrated
Del: useless -- https://master.php.net/note/delete/80004/useless
Del: bad code -- https://master.php.net/note/delete/80004/bad+code
Del: spam -- https://master.php.net/note/delete/80004/spam
Del: non-english -- https://master.php.net/note/delete/80004/non-english
Del: in docs -- https://master.php.net/note/delete/80004/in+docs
Del: other reasons-- https://master.php.net/note/delete/80004
Reject -- https://master.php.net/note/reject/80004
Search -- https://master.php.net/manage/user-notes.php