PHP Function: array_rand
The PHP array_rand function is a handy tool for retrieving random elements from an array. This function accepts an array as an argument and returns one or more
The PHP array_rand() function picks one or more keys at random from an array. It does not return the values themselves — it returns keys, which you then use to read the matching elements. This small distinction is the most common source of confusion with the function, so it is worth keeping in mind throughout this page.
array_rand() is the right tool when you need to pick a random item from a list (a random quote, a random banner, a random question) or take a random subset of a larger array without modifying or reordering the original.
Syntax
array_rand(array $array, int $num = 1): int|string|array| Parameter | Description |
|---|---|
$array | The input array to pick from. Must not be empty. |
$num | How many keys to return. Optional, defaults to 1. Must be between 1 and the array's length. |
The return type depends on $num:
- When
$numis1(or omitted), it returns a single key — anintfor a numeric array or astringfor a string key. - When
$numis2or more, it returns an array of keys.
Picking a single element
The most common use is grabbing one random value. Call array_rand() to get a key, then use that key to index back into the array:
array_rand($colors) returns a single random key (an integer index here), which is used to read the matching color. The two steps are often combined on one line: $colors[array_rand($colors)].
Picking several elements
Pass a second argument to get multiple keys back. The result is an array of keys, and the keys are returned in the same order they appear in the original array — only which keys are chosen is random, not their order:
<?php
$colors = ["red", "green", "blue", "yellow", "orange"];
$keys = array_rand($colors, 2); // e.g. [1, 4]
foreach ($keys as $key) {
echo $colors[$key] . "\n";
}
?>Because array_rand() returns keys and not values, the typical way to map them back to values is array_map:
<?php
$colors = ["red", "green", "blue", "yellow", "orange"];
$picked = array_map(fn($k) => $colors[$k], (array) array_rand($colors, 2));
print_r($picked);
?>Return value and string keys
A frequent misconception is that array_rand() always returns integers. It does not — it returns whatever key it picked, preserving its original type. For an associative array, that means string keys:
<?php
$fruit = ["a" => "apple", "b" => "banana", "c" => "cherry"];
$key = array_rand($fruit);
echo $key; // e.g. "b" (a string key)
echo "\n";
echo $fruit[$key]; // e.g. "banana"
?>Common gotchas
- It returns keys, not values.
array_rand($arr)gives you a key; you still need$arr[$key]to get the value. - Empty array. Calling
array_rand()on an empty array throws aValueError(PHP 8+) or emits a warning and returnsnullon older versions. Check the array first. $numout of range. Asking for more elements than the array contains raises aValueError.- Not cryptographically secure.
array_rand()is fine for shuffling banners or sampling data, but never use it to pick session tokens, passwords, or anything security-sensitive. Userandom_int()with a CSPRNG for that.
Related functions
shuffle()— randomly reorder all elements of an array in place.str_shuffle()— randomly shuffle the characters of a string.rand()andmt_rand()— generate a random integer in a range.array_slice()— extract a contiguous portion of an array.
Conclusion
array_rand() is a compact way to pick random keys from an array — one when you omit $num, an array of them when you pass a count. Remember that it returns keys (preserving their original type, integer or string), that the chosen keys keep the array's original order, and that it is not suitable for security-sensitive randomness.