Get random array value php


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 = 110

Example

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

Get random array value php

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

We can create a six digit random number generator which will give us only characters.

srand ((double) microtime() * 10000000);
$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
Get random array value php



Get random array value php

plus2net.com

Getting a random element from an array

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

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)];
}

How would you get a random element from 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.

How do you generate a random array value?

The task is to select the random element from the array using JavaScript..
Use Math. random() function to get the random number between(0-1, 1 exclusive)..
Multiply it by the array length to get the numbers between(0-arrayLength)..
Use Math. floor() to get the index ranging from(0 to arrayLength-1)..

What is Array_rand function in PHP?

The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.

How do you shuffle an associative array in PHP?

The shuffle() Function is a builtin function in PHP and 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.