Php Array Random Function

What is array_rand Function?

Explanation

The "array_rand" function returns a random element key from an array, or it returns an array of random keys if required. This has to be specified.

Syntax:


array_rand(array,number)

In the above syntax "array" specifies the array from which the "number" of keys to be displayed, by default is its 1.

Example :


<?php
$b=array("c"=>"Cherry","b"=>"Strawberry");
print_r(array_rand($b,1));
?>
Result :

b

In the above example a random key is displayed as the "number" of keys is specified as 1.

PHP Topics


Ask Questions

Ask Question