array_rand
Function • Params and return types changed in PHP 8.0
Pick one or more random keys out of an array.
array_rand Function synopsis
array_rand(array $array, int $num = 1): array|string|int
Parameters
$array
Typearray
The input array. Cannot be empty.
$num
OptionalTypeintDefault value1
Specifies how many entries should be picked.
Must be greater than zero, and less than or equal to the length of $array
Return value
Typearray|string|int
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. If multiple keys are returned,
they will be returned in the order they were present in the original array.
Changes to the array_rand Function
PHP 8.0
- Return type added:
array|string|int - Parameter name of parameter #1 changed:
$argto$array - Parameter type added for parameter #1 (
$array):array - Parameter name of parameter #2 changed:
$num_reqto$num - Parameter type added for parameter #2 (
$num):int - Parameter default value added for position #2 (
$num):1
- array_rand($arg, $num_req)
+ array_rand(array $array, int $num = 1): array|string|int array_rand Function Availability
array_rand Function Availability| PHP Version | Availability |
|---|---|
| PHP 8.6Upcoming Release | Yes |
| PHP 8.5Supported (Latest) | Yes |
| PHP 8.4Supported | Yes |
| PHP 8.3Security-Fixes Only | Yes |
| PHP 8.2Security-Fixes Only | Yes |
| PHP 8.1Unsupported | Yes |
| PHP 8.0Unsupported | Yes |
| PHP 7.4Unsupported | Yes |
| PHP 7.3Unsupported | Yes |
| PHP 7.2Unsupported | Yes |
| PHP 7.1Unsupported | Yes |
| PHP 7.0Unsupported | Yes |
| PHP 5.6Unsupported | Yes |
| PHP 5.5Unsupported | Yes |
| PHP 5.4Unsupported | Yes |
| PHP 5.3Unsupported | Yes |