
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to get random value out of an array in PHP?
To get random value out of an array in PHP, the code is as follows−
Example
<?php $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" ); echo "Array values ...
"; echo "Value 1 = " . $arr["p"], "
"; echo "Value 2 = " . $arr["q"], "
"; echo "Value 3 = " . $arr["r"], "
"; echo "Value 4 = " . $arr["s"], "
"; echo "Value 5 = " . $arr["t"], "
"; echo "Value 6 = " . $arr["u"], "
"; echo "Value 7 = " . $arr["v"], "
"; echo "Value 8 = " . $arr["w"], "
"; 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 −
<?php $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" ); echo "Array values ...
"; echo "Value 1 = " . $arr["p"], "
"; echo "Value 2 = " . $arr["q"], "
"; echo "Value 3 = " . $arr["r"], "
"; echo "Value 4 = " . $arr["s"], "
"; echo "Value 5 = " . $arr["t"], "
"; echo "Value 6 = " . $arr["u"], "
"; echo "Value 7 = " . $arr["v"], "
"; echo "Value 8 = " . $arr["w"], "
"; $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
- Related Articles
- 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?
- How to get numeric index of associative array in PHP?
- How to get the length of longest string in a PHP array
- Search for partial value match in an Array in PHP
- How to re-index an array in PHP?
- Get Random value from a range of numbers in JavaScript?
- How to shuffle an array in a random manner in JavaScript?
- JavaScript - How to pick random elements from an array?
- How to convert an array to SimpleXML in PHP?
- Get closest number out of array JavaScript
- How to get value from serialized array in jQuery?
- PHP – How to get the Unicode point value of a given character?
- How to get an attribute value of an element in Selenium Webdriver?

Advertisements