note 103096 deleted from function.shuffle by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 Mar 2011 14:10:26 +0000 |
| Subject: | note 103096 deleted from function.shuffle by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178201@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
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]);
}
}
?>