How to get a random value from a PHP array? explain with example

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

There are two types of getting a random value from a PHP array.

  • array_rand() function
  • shuffle() function

array_rand() function

When working with PHP, sometimes you need to get random values from an array. This can be easily accomplished with the array_rand() function. We will show in this post several methods that can be used to get random items from an array.

The array_rand() function is used to get a random value from an array. This function accepts an array as a parameter and returns a random key from the array.

Syntax:-

array_rand($array, $number_of_items)

Parameters

The array_rand() function takes two parameters. One is the array from which you want to get the random key.

$array: [Required] This is a required parameter that array_rand() takes.

$number_of_items: [Optional] This is the int value that shows how many random keys you want to get from the array. Its value is set to 1 by default.

Example:-

<?php
    $my_arr = ["Vijay", "Amit", "Dharmu", "Rahul", "Ravi"];

    $rand_index = array_rand($my_arr);

    echo $my_arr[$rand_index];
?>

Output:-

The output will be different each time you execute the above code. If you want to get more than 1 random key from the array you can use the below code example.

Example:

<?php
    $my_arr = ["Vijay", "Amit", "Dharmu", "Rahul", "Ravi"];

    $rand_index = array_rand($my_arr, 2);

    echo $my_arr[$rand_index[0]];
    echo $my_arr[$rand_index[1]];
?>

Output:-

shuffle() function

If you’re looking for a quick and easy way to get a random item from an array using PHP, the shuffle() function is the way to go. This function will randomly shuffle the items in an array, making it easy to get a random item.

Syntax:-

shuffle($arr)[0];

Example:-

<?php
    $my_arr = [
        "a" => 100,
        "b" => 200,
        "c" => 300,
        "4" => 400
    ];

    shuffle($my_arr);

    echo $my_arr[0];
?>

Output

Related Posts

PHP – strip_tags() Replace with Spaces Example

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to Get Duplicate Values from Array in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

PHP explode() Function Example Tutorial

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to Convert Array to String in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How Can I Convert a String in PHP Into an Array?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to Use the array_merge() Function in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x