24th January 2008 - 4 minutes read time
To randomise an array in PHP use the shuffle() function like this.
$array = range(1, 5);
shuffle($array); // randomise array
There are two limitations to this function. The first is that it completely removes any key association that you may have set up. So the following array.
$array = array('one'=>1,'two'=>2);
Would be turned into the following after using shuffle().
Array
(
[0] => 2
[1] => 1
)
A way around this is to use the following function, this uses the PHP function array_rand() to extract the all of the keys in the array in a random order before ensuring that the key associations are in place.