php basics
PHP array_rand Take out the array unit
PHP array_rand can be used to fetch array elements, one or more array elements at a time. Note that array_rand fetches the key value of the array, not the value of the element. If you want to fetch the value of the array element, you can use array_rand first After obtaining the key value, use array output to obtain the element value.
PHP array_rand syntax example
array_rand (input array, int $num_req);
The input array in the example, as the name implies, must be an array ( PHP Array ). If it is just a simple string, it cannot be processed. The following $num_req is used to tell the system how many units you want to take out. The number format, the default is 1. The number range of $num_req cannot be written randomly. For example, if the array has only 5 elements, it will be wrong if it is written as 6, and writing 0 will also cause an error, because it represents meaningless data.
PHP array_rand implementation example
<?php
$Arr = array("Apple", "Watermelon", "Banana", "Grapes", "Pineapple");
$RandKey = array_rand($Arr,2);
print ($RandKey[0]).' <br>';
print $Arr[$RandKey[0]]. "<br>";
?>
$Arr = array("Apple", "Watermelon", "Banana", "Grapes", "Pineapple");
$RandKey = array_rand($Arr,2);
print ($RandKey[0]).' <br>';
print $Arr[$RandKey[0]]. "<br>";
?>
At the beginning of the example, a five-element array $Arr is prepared. $RandKey obtains two key values at will through array_rand, and then two arrays are output. The first array output print ($RandKey[0]) will output The number from 0 to 4 is the key value randomly taken out, and the second array output print $Arr[$RandKey[0]] is to apply the key value just output to the original array $Arr to obtain the key The element of the value.
Post a Comment
0 Comments