note 41047 deleted from function.shuffle by aidan

From: Date: Thu, 26 Aug 2004 22:32:52 +0000
Subject: note 41047 deleted from function.shuffle by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75364@lists.php.net to get a copy of this message
Note Submitter: chvickers at hotmail dot com ---- we all know the shuffle() is bad for shuffeling normal arrays and even worse for shuffeling associate multi arrays to avoid loosing your associative keys in an associative multidimensional array use this shuffle function: (it is not perfect but it works) function mix($org_arr) { $keys = array_keys($org_arr); $max = count($org_arr)-1; for ($y = 1; $y <= pow($max+1,3); $y++) { mt_srand((double)microtime()*1000000); $r = mt_rand(0,$max); $new_arr[$keys[$r]] = $org_arr[$keys[$r]]; } return $new_arr; } function swapshuffle($array) { srand ((double) microtime() * 10000000); for ($i=0;$i<sizeof($array);$i++) { $from=rand(0,sizeof($array)-1); $old=$array[$i]; $array[$i]=$array[$from]; $array[$from]=$old; } return $array; } function submix($org_arr) { $keys = array_keys($org_arr); $max = count($org_arr)-1; for ($y = 0; $y <= $max; $y++) { $new_arr[$keys[$y]] = swapshuffle($org_arr[$keys[$y]]); } return $new_arr; } $boxes = submix($boxes); # shuffles the subarrays $boxes = mix($boxes); # shuffles the main array

« previous php.notes (#75364) next »