PHP array_rand() Function
Last Updated : 17 Mar 2025
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
| Parameter | Description | Required/Optional |
|---|---|---|
| array | Specifies an array. | compulsory |
| number | Specifies 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
Next TopicPhp-array-range-function