note 4041 modified in usort by ronabop

From: Date: Wed, 18 Oct 2000 05:44:05 +0000
Subject: note 4041 modified in usort by ronabop
Groups: php.notes 
Request: Send a blank email to php-notes+get-6647@lists.php.net to get a copy of this message
As a correction to the piece of code donated by ccunning@math.ohio-state.edu above, this function will randomize an array passed by reference. The previous version decreased the length of the array by one. Greg <?php function myshuffle($array) { mt_srand((double) microtime() * 1000000); $num = count($array); for ($i = 0; $i < $num; $i ++) { $n = mt_rand(0, $num - 1); // Swap the data. $temp = $array[$n]; $array[$n] = $array[$i]; $array[$i] = $temp; } // ends for } // ends function myshuffle(&array) // Test the results. $array = array(1,2,3,4,5,6,7,8,9,10); myshuffle(&$array); while (list(,$var)=each($array)) echo $var . " "; ?> http://www.php.net/manual/function.usort.php

« previous php.notes (#6647) next »