note 102995 added to function.shuffle

From: Date: Sat, 19 Mar 2011 03:22:36 +0000
Subject: note 102995 added to function.shuffle
Groups: php.notes 
Request: Send a blank email to php-notes+get-177996@lists.php.net to get a copy of this message
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. ---- Server IP: 192.41.42.26 Probable Submitter: 24.53.177.177 ---- Manual Page -- http://www.php.net/manual/en/function.shuffle.php Edit -- https://master.php.net/note/edit/102995 Del: integrated -- https://master.php.net/note/delete/102995/integrated Del: useless -- https://master.php.net/note/delete/102995/useless Del: bad code -- https://master.php.net/note/delete/102995/bad+code Del: spam -- https://master.php.net/note/delete/102995/spam Del: non-english -- https://master.php.net/note/delete/102995/non-english Del: in docs -- https://master.php.net/note/delete/102995/in+docs Del: other reasons-- https://master.php.net/note/delete/102995 Reject -- https://master.php.net/note/reject/102995 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177996) next »