note 103065 deleted from function.shuffle by danbrown
| From: | danbrown@php.net | Date: | Wed, 23 Mar 2011 15:10:10 +0000 |
| Subject: | note 103065 deleted from function.shuffle by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178125@lists.php.net to get a copy of this message | ||
Note Submitter: tim at leethost dot com
----
Update to Rick's seoShuffle but 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 an additional
value to be passed to set the random seed (uses 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.
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]);
}
}