To get random value out of an array in PHP, the code is as follows− Show ExampleLive 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)]; ?> OutputThis 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 ExampleLet 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]]; ?> OutputThis 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
PHP Random collection of element from an arrayWe 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. We can create a six digit random number generator which will give us only characters. 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 plus2net.com 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: 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 = [1, 2, 3, 4, 5]; // ^ ^ ^ ^ ^ // indexes | | | | | // 0 1 2 3 4
Keeping this in mind, lets get a random index from our $randomIndex = mt_rand(0, count($numbers) - 1); Luckily, PHP has a built-in function to do all this thinking for us, $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 $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.
|