Random numbers can be generated using rand (). However, I want to randomly select one of several fixed options. Is there such a function? For example, the options include Beijing, Shanghai, and Guangzhou. You can randomly select one of the three fixed options. Random numbers can be generated using rand (). However, I want to randomly select one of several fixed options. Is there such a function?
For example, the options include Beijing, Shanghai, and Guangzhou. You can randomly select one of the three fixed options.
Reply content:
Random numbers can be generated using rand (). However, I want to randomly select one of several fixed options. Is there such a function?
For example, the options include Beijing, Shanghai, and Guangzhou. You can randomly select one of the three fixed options.
Your options should be an array, which can be implemented using array_rand.
The array_rand () function randomly selects one or more elements from the array and returns them.
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.
W3school example:
$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Print_r (array_rand ($ a, 1 ));
?>
Output:
B