The array_rand () function is described in the document as follows: {code...} parameter input array. Num_req indicates the number of units you want to fetch. If the specified number exceeds the number in the array, an E_WARNING error is generated. If you retrieve only one... array_rand () function in this document:
mixed array_rand ( array $input [, int $num_req = 1 ] )
Parameters
Input
Input array.
Num_req
Specify the number of units you want to fetch. If the specified number exceeds the number in the array, an E_WARNING error is generated.
Return Value
If you only retrieve one key, array_rand () returns the key name of a random unit. Otherwise, an array containing the random key name is returned. In this way, you can randomly retrieve the key name and value from the array. Extract one or more random units from the array and return one or more keys of the random entries.
So, will the key names of the obtained array be repeated? It seems that this is not explained for a long time on the Internet.
Reply content:
The array_rand () function is described in the document as follows:
mixed array_rand ( array $input [, int $num_req = 1 ] )
Parameters
Input
Input array.
Num_req
Specify the number of units you want to fetch. If the specified number exceeds the number in the array, an E_WARNING error is generated.
Return Value
If you only retrieve one key, array_rand () returns the key name of a random unit. Otherwise, an array containing the random key name is returned. In this way, you can randomly retrieve the key name and value from the array. Extract one or more random units from the array and return one or more keys of the random entries.
So, will the key names of the obtained array be repeated? It seems that this is not explained for a long time on the Internet.
There will never be duplicates. I will paste the main part of the source code. The algorithm loops through the elements in the array in sequence and compares them with algorithms one by one. when the conditions are met, the key value is placed in the return value, until the number of keys that meet the conditions is obtained or the array element is cyclically completed.
.
php
// The key name should not be repeated $ arr = [, 3]; $ res = array_rand ($ arr, 3); // always, 2] echo $ arr [$ res [0]; // 1 echo $ arr [$ res [0]; // 2 echo $ arr [$ res [0]; // 3
Shuffle is recommended for random elements.array_rand
Http://www.php.net/manual/en/function.array-rand.php
The key names are certainly not repeated.
The key names of the array will never be repeated.