Shuffle and array_rand are defined and used. For more information, see.
Shuffle and array_rand are defined and used. For more information, see.
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.
Example
The Code is as follows:
$ 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 1
The Code is as follows:
$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Print_r (array_rand ($ a, 1 ));
?>
Output:
B
Example 2
Arrays with string keys:
The Code is as follows:
$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Print_r (array_rand ($ a, 2 ));
?>
Output:
Array ([0] => c [1] => B)