note 102995 deleted from function.shuffle by danbrown
| From: | danbrown@php.net | Date: | Sat, 19 Mar 2011 15:23:33 +0000 |
| Subject: | note 102995 deleted from function.shuffle by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178007@lists.php.net to get a copy of this message | ||
Note Submitter: tim at leethost dot com
----
rick at suggestive dot com's seoShuffle was exactly what I needed. But, I modified it to make
it easier to use (no $string variable needed) and more shuffled (seed is a CRC of first value in the
source array). As in the original, it yields consistent yet shuffled results. Further, this new
function guarantees the shuffled results are in a different order than the original, which further
assists SEO purposes. Here's the updated function:
function seoShuffle(&$items) {
$original = md5(serialize($items));
mt_srand(abs(crc32($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]);
}
}
*NOTE: I realize this is not getting the actual CRC32 of the first item in the array, it
doesn't matter for this purpose. The purpose is to just get a large integer that's very
unique based on a string, and for this purpose it's more than sufficient. Also, this function
was designed to be backward-compatible, effective, and tight. For example, serialized was used
instead of json_encode because serialized works in older versions of PHP. If you have the need to
get every last drop of speed, feel free to make potential performance modifications.