PHP array_rand() Function

The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.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.Picks one or more random entries out of an array, and returns the key (or keys) of the random entries. It uses a pseudo random number generator that is not suitable for cryptographic purposes.

When picking only one entry, array_rand() returns the key for a random entry. Otherwise, an array of keys for the random entries is returned. This is done so that random keys can be picked from the array as well as random values. Trying to pick more elements than there are in the array will result in an E_WARNING level error, and NULL will be returned.

Example -

Example -