note 103096 added to function.shuffle
| From: | Anonymous at osu1 dot php dot net | Date: | Fri, 25 Mar 2011 14:09:58 +0000 |
| Subject: | note 103096 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178200@lists.php.net to get a copy of this message | ||
Update to Rick's seoShuffle that's been enhanced in two ways. First, it *guarantees*
shuffled yet consistent order which further benefits SEO purposes (Rick's function would
sometimes yield an identical result order, which defeats the purpose). Secondly, it no longer
requires the seed variable to be passed to set the random seed (will use the first value in the
array as the seed if not specified). The function has the same purpose as the original--to shuffle
the array, but to do it identically each time it's shuffled.
<?php
/* $seed variable is optional */
function seoShuffle(&$items, $seed=false) {
$original = md5(serialize($items));
mt_srand(crc32(($seed) ? $seed : $items[0]));
for ($i = count($items) - 1; $i > 0; $i--){
$j = @mt_rand(0, $i);
list($items[$i], $items[$j]) = array($items[$j], $items[$i]);
}
if ($original == md5(serialize($items))) {
list($items[count($items) - 1], $items[0]) = array($items[0], $items[count($items) - 1]);
}
}
?>
----
Server IP: 192.41.42.26
Probable Submitter: 72.240.52.67
----
Manual Page -- http://www.php.net/manual/en/function.shuffle.php
Edit -- https://master.php.net/note/edit/103096
Del: integrated -- https://master.php.net/note/delete/103096/integrated
Del: useless -- https://master.php.net/note/delete/103096/useless
Del: bad code -- https://master.php.net/note/delete/103096/bad+code
Del: spam -- https://master.php.net/note/delete/103096/spam
Del: non-english -- https://master.php.net/note/delete/103096/non-english
Del: in docs -- https://master.php.net/note/delete/103096/in+docs
Del: other reasons-- https://master.php.net/note/delete/103096
Reject -- https://master.php.net/note/reject/103096
Search -- https://master.php.net/manage/user-notes.php