Backend Development 5 min read

Using PHP's array_rand() Function to Randomly Select Elements from Arrays

This article explains PHP's array_rand() function, its syntax and parameters, and provides three practical examples showing how to randomly pick one or multiple values from indexed and associative arrays, complete with code snippets and expected output.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's array_rand() Function to Randomly Select Elements from Arrays

array_rand() is a very useful PHP function that can randomly select one or more elements from an array, making it convenient for scenarios where random array items are needed.

The basic syntax of array_rand() is:

mixed array_rand ( array $array [, int $num = 1 ] )

Parameter description:

$array: required, the input array from which elements will be selected.

$num: optional, the number of elements to pick; defaults to 1.

If $num is 1, the function returns the key of the selected element.

If $num is greater than 1, the function returns an array of keys of the selected elements.

Example 1: Randomly select a single element from an array

Assume we have an array of city names and want to display a randomly chosen city.

$cities = array("New York", "London", "Paris", "Tokyo", "Beijing");
$randomCity = array_rand($cities);
echo "Today's featured city is: " . $cities[$randomCity];

Sample output:

Today's featured city is: Paris

Example 2: Randomly select multiple elements from an array

We can also ask array_rand() to pick several elements at once. The following example selects three random cities.

$cities = array("New York", "London", "Paris", "Tokyo", "Beijing");
$randomCities = array_rand($cities, 3);
foreach($randomCities as $key) {
    echo $cities[$key] . "
";
}

Sample output:

London
Tokyo
New York

Example 3: Randomly select an element from an associative array

Beyond indexed arrays, array_rand() works with associative arrays as well. The example below picks a random celebrity and displays their age.

$celebrities = array(
    "Tom Hanks" => 64,
    "Brad Pitt" => 57,
    "Jennifer Aniston" => 52,
    "Meryl Streep" => 71,
    "Johnny Depp" => 58
);
$randomCelebrity = array_rand($celebrities);
echo "Today's celebrity is: " . $randomCelebrity . ", Age: " . $celebrities[$randomCelebrity];

Sample output:

Today's celebrity is: Johnny Depp, Age: 58

Conclusion

The array_rand() function is a practical tool in PHP for randomly selecting one or more elements from both indexed and associative arrays; you simply provide the array variable and optionally the number of elements to retrieve.

We hope the example code above helps you understand how to use array_rand() effectively in your projects.

backend developmentarraysRandomarray_rand
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.