To get random value out of an array in PHP, the code is as follows−
Example
Live Demo
<?php $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" ); echo "Array values ...\n"; echo "Value 1 = " . $arr["p"], "\n"; echo "Value 2 = " . $arr["q"], "\n"; echo "Value 3 = " . $arr["r"], "\n"; echo "Value 4 = " . $arr["s"], "\n"; echo "Value 5 = " . $arr["t"], "\n"; echo "Value 6 = " . $arr["u"], "\n"; echo "Value 7 = " . $arr["v"], "\n"; echo "Value 8 = " . $arr["w"], "\n"; echo "Random value from arary = ".$arr[array_rand($arr)]; ?>Output
This will produce the following output −
Array values ... Value 1 = 150 Value 2 = 100 Value 3 = 120 Value 4 = 110 Value 5 = 115 Value 6 = 103 Value 7 = 105 Value 8 = 125 Random value from arary = 110Example
Let us now see another example −
Live Demo
<?php $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" ); echo "Array values ...\n"; echo "Value 1 = " . $arr["p"], "\n"; echo "Value 2 = " . $arr["q"], "\n"; echo "Value 3 = " . $arr["r"], "\n"; echo "Value 4 = " . $arr["s"], "\n"; echo "Value 5 = " . $arr["t"], "\n"; echo "Value 6 = " . $arr["u"], "\n"; echo "Value 7 = " . $arr["v"], "\n"; echo "Value 8 = " . $arr["w"], "\n"; $res = array_rand($arr, 2); echo "Random values from array..."; echo $arr[$res[0]]." ".$arr[$res[1]]; ?>Output
This will produce the following output−
Array values ... Value 1 = 150 Value 2 = 100 Value 3 = 120 Value 4 = 110 Value 5 = 115 Value 6 = 103 Value 7 = 105 Value 8 = 125 Random values from array...150 115
Updated on 24-Dec-2019 12:15:57
- Related Questions & Answers
- How to get the first element of an array in PHP?
- Get the closest number out of an array in JavaScript
- Generate array of random unique numbers in PHP?
- Get closest number out of array JavaScript
- Get Random value from a range of numbers in JavaScript?
- How to get numeric index of associative array in PHP?
- JavaScript - How to pick random elements from an array?
- How to get an attribute value of an element in Selenium Webdriver?
- Search for partial value match in an Array in PHP
- How to capture out of array index out of bounds exception in Java?
- Get a random value between two values in MySQL?
- How to shuffle an array in a random manner in JavaScript?
- How to get an attribute value in jQuery?
- How to get the most out of Canva?
- How to re-index an array in PHP?
PHP Random collection of element from an array
We can randomly collect one or more element from an array by using array_rand() function in PHP. We can specify the number of random elements required by specifying an optional parameter in side the function. $value=array("Rabin","Reid","Cris","KVJ","John");
$rand_keys=array_rand($value,2);
echo "First random element = ".$value[$rand_keys[0]];
echo "<br>Second
random element = ".$value[$rand_keys[1]];
$input = array ("A", "B", "C", "D", "E","F","G","H","I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z");
$rand_index = array_rand($input,6);
print $input[$rand_index[3]];
print $input[$rand_index[5]];
print $input[$rand_index[4]];
print $input[$rand_index[2]];
print $input[$rand_index[1]];
print $input[$rand_index[0]];
By using above code we can generate 6 digit random number of only characters. You can read how to generate random numbers here.
Read here how to generate random password string having alphabets and digits
tldr: the function at the end of this gist will give you a random array element Figuring out how to get a random array element will give a better understanding of how PHP works, as well as how arrays work in general. Lets start with this code: $numbers = [1, 2, 3, 4, 5];Getting a random element from an array
And get a random number out of it!
First we need a random index out of the array.
Remember that arrays are 0 - indexed, and the largest index is one less than the number of items in the array.
Using our $numbers array as an example:
$numbers = [1, 2, 3, 4, 5]; // ^ ^ ^ ^ ^ // indexes | | | | | // 0 1 2 3 4
- The first element, 1 is at index 0, and the last element, 5 is at index 4
- The total number of elements in the array is 5
- 4, the greatest index in the array, is one less than 5, the total number of elements in the array.
Keeping this in mind, lets get a random index from our $numbers array. We'll use PHP's build-in count function that returns the number of items in an array.
$randomIndex = mt_rand(0, count($numbers) - 1);
Luckily, PHP has a built-in function to do all this thinking for us, array_rand. All we have to do is give array_rand an array, and it will return us a random index in it. We could rewrite our code like so.
$randomIndex = array_rand($numbers);
Now we need to access the element at our random index.
$randomNumber = $numbers[$randomIndex];
And we have a random number from our array!
Lets pull this code out into a separate function that we can reuse.
function getRandomArrayElement($array) { $randomIndex = array_rand($array); $randomElement = $array[$randomIndex]; return $randomElement; }
Now to get a random number from our $numbers array, all we would have to do is call our function.
$randomNumber = getRandomArrayElement($numbers);
We could also rewrite our function to be a one liner:
function getRandomArrayElement($array) { return $array[array_rand($array)]; }