note 4041 deleted from function.usort by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 16:33:36 +0000 |
| Subject: | note 4041 deleted from function.usort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176091@lists.php.net to get a copy of this message | ||
Note Submitter: gfaron at integretechpub dot com
----
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 . " ";
?>