How to get random value out of an array in PHP?
Last Updated :
19 Sep, 2024
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: Using 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: In this 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.
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];
?>
Method 2: Use 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: In this 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.
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];
?>
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]];
?>
Similar Reads
How to Randomize the Order of an Array in PHP ? Randomizing the order of an array is a common operation in PHP, especially when dealing with datasets or when you want to shuffle the presentation of items. In this article, we will explore various approaches to randomize the order of an array.Table of ContentUsing shuffle() FunctionUsing usort() Fu
3 min read
How to print all the values of an array in PHP ? We have given an array containing some array elements and the task is to print all the values of an array arr in PHP. In order to do this task, we have the following approaches in PHP:Table of ContentUsing for-each loop: Using count() function and for loop: Using implode():Using print_r() FunctionUs
3 min read
How to Create an Array of Given Size in PHP? This article will show you how to create an array of given size in PHP. PHP arrays are versatile data structures that can hold multiple values. Sometimes, it's necessary to create an array of a specific size, either filled with default values or left empty. Table of Content Using array_fill() Functi
3 min read
How to Print Unique Elements an Given Array in PHP? Given an array, the task is to print all distinct elements of the given array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted.Examples: Input: arr[] = {5, 6, 5, 3, 6, 4, 3, 5}Output: 5, 6, 3, 4Input: arr[] = {1, 2, 3, 4, 5, 5
2 min read
How to get the First Element of an Array in PHP? Accessing the first element of an array in PHP is a common task, whether they are indexed, associative, or multidimensional. PHP provides various methods to achieve this, including direct indexing, and built-in functions.Below are the approaches to get the first element of an array in PHP:Table of C
4 min read