array_rand()
If you want to pick out one element at random from an array, array_rand() is the way to go. It doesn’t touch the original array; that remains in the same order as it began.
What’s important to remember here is that it returns the key not the value.
You can also say how many random keys you want. In this case you’ll get an array returned to you. This number must be less than or equal to the number of elements in the array (and greater than zero).
$arr = array('apple', 'banana', 'cucumber', 'dish'=>'bowl', 'mango'); $x = array_rand($arr); // returns one of [0,1,2,'dish',3] $y = array_rand($arr, 2); // returns an array of two of those keys in random order $z = array_rand($arr, count($arr)); // returns the entire array in a new random order like shuffle() but leaving the original array
