Shuffle () Definition and usage
The shuffle () function sorts the elements in the array in random order.
If yes, true is returned. Otherwise, false is returned.
Note: This function assigns a new key name to the cell in the array. This will delete the original key name, not just the reorder.
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
Shuffle (array) parameter description
Array is required. Specifies the array to be used.
ExampleCopyCodeThe Code is as follows: <? PHP
$ My_array = array ("A" => "dog", "B" => "cat", "C" => "horse ");
Shuffle ($ my_array); print_r ($ my_array );
?>
Output:
Array ([0] => CAT [1] => horse [2] => dog)
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: If the number of indexes extracted by the specified array_rand () function is greater than 1, the keys of the original array will be obtained no matter whether the index array or associated array is extracted, and put it in a new index array.
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 1Copy codeThe Code is as follows: <? PHP
$ A = array ("A" => "dog", "B" => "cat", "C" => "horse ");
Print_r (array_rand ($ A, 1 ));
?>
Output:
B
Example 2
Arrays with string keys:Copy codeThe Code is as follows: <? PHP
$ A = array ("A" => "dog", "B" => "cat", "C" => "horse ");
Print_r (array_rand ($ A, 2 ));
?>
Output:
Array ([0] => C [1] => B)