How can random values be selected from an array in PHP?

To select random values from an array in PHP, you can use the array_rand() function. This function returns one or more random keys from the array, which can then be used to access the corresponding values. By using array_rand(), you can easily select random elements from an array without the need for complex logic.

// Define an array
$array = array('apple', 'banana', 'cherry', 'date', 'elderberry');

// Select a random key from the array
$randomKey = array_rand($array);

// Access the corresponding value using the random key
$randomValue = $array[$randomKey];

echo $randomValue;