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)
ParameterDescription
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

<?php
$arr=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4");
print_r(array_rand($arr,1));
?>

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

<?php
$arr=array("blue","green","red","pink","brown");
$random=array_rand($arr,3);
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
en English
X
Scroll to Top