Array_rand ()- random extraction of one or more cells from an array
Mixed Array_rand Array $input $num _req = 1])
Extracts one or more random cells from an array and returns one or more keys for a random entry.
Parameters
-
input:
The input array. Necessary. Specifies the array.
-
num_req:
Indicates how many units you want to take out of the unit. Optional. Specifies how many random key names to return. If the number specified exceeds the number in the array, a E_WARNING level of error will occur.
Example 1
Returns a random key from the array:
<? PHP $a=array("a" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"); Print_r (array_rand($a, 1));? >
Example 2
Returns an array containing a random string key name:
<? PHP $a=array("a" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"); Print_r (array_rand($a, 2));? >
One random result:
Array
Two random results:
Array
Official Website Example:
Example #1 Array_rand () example
<? PHP $input Array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand _keys Array_rand ($input, 2); Echo $input [$rand _keys[0]]. "\ n"; Echo $input [$rand _keys[1]]. "\ n";? >
Array_rand () randomly extracts one or more cells from an array