note 103065 added to function.shuffle

From: Date: Wed, 23 Mar 2011 14:43:45 +0000
Subject: note 103065 added to function.shuffle
Groups: php.notes 
Request: Send a blank email to php-notes+get-178122@lists.php.net to get a copy of this message
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]); } } ---- Server IP: 208.69.120.55 Probable Submitter: 72.240.52.67 ---- Manual Page -- http://www.php.net/manual/en/function.shuffle.php Edit -- https://master.php.net/note/edit/103065 Del: integrated -- https://master.php.net/note/delete/103065/integrated Del: useless -- https://master.php.net/note/delete/103065/useless Del: bad code -- https://master.php.net/note/delete/103065/bad+code Del: spam -- https://master.php.net/note/delete/103065/spam Del: non-english -- https://master.php.net/note/delete/103065/non-english Del: in docs -- https://master.php.net/note/delete/103065/in+docs Del: other reasons-- https://master.php.net/note/delete/103065 Reject -- https://master.php.net/note/reject/103065 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178122) next »