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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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];
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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]];
  
?>

chevron_right


Output:

21 7


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.