PHP array_rand() function

PHP array_push() function
PHP array_reduce() function

The array_rand function returns a random key or array of random keys (in the case of multiple keys) from an array. You will get more understanding after going through the syntax and example of the function.

What is the syntax of the array_rand function in PHP?

array_rand(array, number)
arrayThe array from which the random key is to be selected – Required
numberNumber of random keys to return from array – Optional
array_rand function in PHP

Examples of array_rand function


In the above example, the output will contain random keys from the given array.

echo $a[$random[0]];
echo $a[$random[1]];
echo $a[$random[2]];

In the above example, we return multiple random keys from the array.

PHP array_push() function
PHP array_reduce() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z