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

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

Associative Array in PHP?

An associative array is a simple and very important array of PHP. This array is also used the most, it is very easy to understand this array,…

How to work in functions PHP?

If there is any task in your program which you train to execute repeatedly then instead of writing the code for that task at a different place…

For Loofs in PHP

In for-loop initialization, condition and increment happen on the same line. The loop keeps on repeating as long as the condition is true. When the for loop…

If Else Conditionals in Php?

If Else statement is the most important feature of any programming language, PHP is used to run any Code Of Block according to If Else conditions. This…

What are operators in PHP? PHP – Operator Types

Operators are used to developing data. In Programming Languages, Operators are taken from Mathematics and Operator Programmers work with Data. An operator is a chain of symbols…

String Functions in PHP?

When many characters are written together in sequence, they form a string. For example, India is a string. It is made up of i, n, d, i,…

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