The array_rand() array function in php will select one or more array items from the array at random. The array_rand() array function accepts one or two arguments . First is the array and second one is the number of items to display from an array . By default the array function displays one item from an array.
Syntax:
array_rand(array); // Displays 1 item from array or array_rand(array, no_of_items); // displays no_of_items from an array
Example:
<?php $targetArray = array("Sara","Cindy","Julie","Megan"); $rand = array_rand($targetArray, 2); foreach ($rand as $key => $value) { echo "$key - <strong>" . $targetArray[$value] . "</strong><br />"; } ?>
Output:
0 - Julie 1 - Megan