Array_rand () is used to randomly extract one or more units from an array. It is useful to retrieve one or more random units from an array. It accepts input as an input array and an optional parameter num_req, specifying the number of units you want to retrieve-if not specified, the default value is 1. If you only retrieve one, array_rand () returns the key name of a random unit. Otherwise, an array containing the random key name is returned.
<? Php Tutorial
Srand (float) microtime () * 10000000 );
$ Input = array ("neo", "morpheus", "trinity", "cypher", "tank ");
$ Rand_keys = array_rand ($ input, 2 );
Print $ input [$ rand_keys [0]. "n ";
Print $ input [$ rand_keys [1]. "n ";
?>
Array_rand () definition and usage
The array_rand () function randomly selects one or more elements from the array and returns them.
The second parameter is used to determine the elements to be selected. If more than one element is selected, an array containing the random key name is returned. Otherwise, the key name of the element is returned.
Note: Since php 4.2.0, the random number generator is no longer needed to be planted using the srand () or mt_srand () function, which is now automatically completed.
Syntax
Array_rand (array, number) parameter description
Array is required. Specify the input array parameters.
Number is optional. The default value is 1. Specifies how many random elements are returned.
Example 1
<? Php
$ A = array ("a" => "dog", "B" => "cat", "c" => "horse ");
Print_r (array_rand ($ a, 1 ));
?>