Introduction to shuffle () and array_rand () random functions in php array function sequence. Shuffle () definition and usage shuffle () function sorts the elements in the array in random order. If yes, TRUE is returned. otherwise, FALSE is returned. Note: This function is a unit in an array.
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)
The http://www.bkjia.com/PHPjc/324498.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324498.htmlTechArticleshuffle () definition and usage shuffle () function sorts the elements in the array in random order. If yes, TRUE is returned. otherwise, FALSE is returned. Note: This function is a unit in the array...