PHP array_rand() Function

The array_rand()function is a inbuilt function in PHP. The array_rand()function is used to fetch one or more random entries from an array. It uses a pseudo-random number generator that is not suitable for cryptographic purposes. This function was introduced in PHP 4.0.

Syntax

Parameter

ParameterDescriptionRequired/Optional
arraySpecifies an array.compulsory
numberSpecifies how many random keys to return.Optional

Return Type

The array_rand() function returns the key for a random entry. Otherwise, it returns an array of keys for the random entries.

Example 1

Output:

b

Example 2

Output:

php
ruby

Example 3

Output:

Array ( [0] => sachin [1] => rahul )

Example 4

Output:

worldcup