PHP array_rand() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_rand() picks one or more random keys from an array.

It returns a single key when num is 1, and an array of keys when num is greater than 1. Read the value afterwards with $array[$key].

Syntax

Call array_rand() with the values you need. Start with this line.

PHP
$key = array_rand($colors);
CallWhat it does
array_rand($colors)Returns one random key, such as 0, 1 or 2.
array_rand($colors, 2)Returns an array holding two different random keys.
$colors[array_rand($colors)]Picks a random value from the array.

The result is random, so it can change on every run. Treat it as "some valid key" and never rely on a specific one.

Library declarations
PHP
array_rand(array $array, int $num = 1): int|string|array

Parameters

ParameterDescription
arrayRequired. The array to pick from. It must not be empty.
numOptional. How many keys to pick. Default is 1. It must be between 1 and the size of the array.

Return Value

Return ValueDescription
int|string|arrayOne key if num is 1, or an array of keys in their original order if num is greater than 1.

PHP Examples

Example 1 — Pick a Random Key

The exact key changes every run, so this example checks what kind of thing comes back.

PHP
<?php
$fruits = ["a" => "apple", "b" => "banana", "c" => "cherry"];

$key = array_rand($fruits);

echo gettype($key), PHP_EOL;
echo array_key_exists($key, $fruits) ? "valid key" : "missing key", PHP_EOL;
echo is_string($fruits[$key]) ? "value is a string" : "other", PHP_EOL;

How It Works

  1. Create an array. The keys are the strings a, b and c.
  2. Call array_rand(). It returns one of those keys, but we do not know which.
  3. Check what came back. It is a string because the keys are strings.
  4. Read the value. Using the key with $fruits[$key] always works.

Example 2 — Pick Several Keys and Empty Arrays

Asking for more than one key returns an array, and an empty array is an error.

PHP
<?php
$fruits = ["apple", "banana", "cherry"];

$keys = array_rand($fruits, 2);

echo count($keys), PHP_EOL;
echo gettype($keys), PHP_EOL;

try {
    array_rand([]);
} catch (ValueError $e) {
    echo "Empty array not allowed", PHP_EOL;
}

How It Works

  1. Ask for 2 keys. The second argument says how many to pick.
  2. Check the result. It is an array holding two different keys.
  3. Try an empty array. There is nothing to pick from.
  4. Catch the ValueError. PHP 8 throws this error, so the message is printed.

Example 3 — Pick a Random Value

Use the key to read a value from a list. The program works whatever key is chosen.

PHP
<?php
$colors = ["red", "green", "blue"];

$index = array_rand($colors);
$pick = $colors[$index];

echo is_int($index) ? "int key" : "string key", PHP_EOL;
echo in_array($pick, $colors, true) ? "Picked a valid color" : "Unknown color", PHP_EOL;

How It Works

  1. Create a list. Its keys are the integers 0, 1 and 2.
  2. Get a random key. It could be any of the three.
  3. Read the value. $colors[$index] gives the color at that key.
  4. Verify the pick. Whatever was chosen is always one of the colors in the list.

Frequently Asked Questions

It picks one or more random keys from an array. You then use the key to read the value.
A key. To get the value, write $array[array_rand($array)].
Pass a second argument, like array_rand($array, 3). You get an array of keys in their original order.
In PHP 8 the array must not be empty and num must be between 1 and the array size.
No. It is not cryptographically secure. Use random_int() or random_bytes() for security uses.

Next: array_reduce()

array_reduce() combines all the items of an array into a single value by running a functio.

array_reduce() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful