Randomly sort an array using PHP’s shuffle() function

If you ever need to randomise a PHP array, and want to do it in one line, use the shuffle() function. It’s especially useful when you just want to draw one array member randomly, like in a lottery or something.

$values = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 
shuffle($values); //randomise the array members
echo array_pop($values); //outputs a random number from $values

Leave a Reply