Open In App

How to get random value out of an array in PHP?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

There are two functions to get random value out of an array in PHP. The shuffle() and array_rand() function is used to get random value out of an array.

Examples: 

Input : $arr = ("a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20")

// Get one random value
Output :7

Input : $arr = ("a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20")

// Get two random values
Output : 21 31

Method 1: This method discuss the shuffle() function to get random value out of an array in PHP.
PHP | shuffle() Function: The shuffle() Function is an inbuilt function in PHP which is used to shuffle or randomize the order of the elements in an array. This function assigns new keys for the elements in the array. It will also remove any existing keys, rather than just reordering the keys and assigns numeric keys starting from zero.

Syntax:  

bool shuffle( $array )

Example:  

PHP




<?php
 
// Declare an associative array
$arr = array( "a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20" );
 
// Use shuffle function to randomly assign numeric
// key to all elements of array.
shuffle($arr);
 
// Display the first shuffle element of array
echo $arr[0];
 
?>


Output: 

31

 

In the above example the keys of associative array have been changed. The shuffle() function has randomly assigned keys to elements starting from zero. As shuffle() permanently change the keys of an array.

Method 2: Use array_rand() function to get random value out of an array in PHP.
PHP | array_rand() Function: The array_rand() function is an inbuilt function in PHP which is used to fetch a random number of elements from an array. The element is a key and can return one or more than one key.

Syntax: 

array_rand( $array, $num )

This function accepts two parameters $array and $num. The $array variable store the array elements and $num parameter holds the number of elements need to fetch. By default value of this parameter is 1.

Example 1:  

PHP




<?php
 
// Declare an associative array
$arr = array( "a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20" );
 
// Use array_rand function to returns random key
$key = array_rand($arr);
 
// Display the random array element
echo $arr[$key];
 
?>


Output: 

21

 

In the above example we didn’t explicitly specified the value for second parameter so by default the value is 1 and array_rand() will return one random key.

Example 2: This example explicitly specify the value for second parameter so array_rand() function will return the array of random keys. 

PHP




<?php
 
// Declare an associative array
$arr = array( "a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20" );
 
// It specify the number of element
$num = 2;
  
// It returns array of random keys
$keys = array_rand( $arr, $num );
 
// Display the array element
echo $arr[$keys[0]]." ".$arr[$keys[1]];
 
?>


Output: 

21 7

 



Last Updated : 27 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads