Shuffle () Definition and usage
The shuffle () function rearranges the elements in the array in random order.
Returns TRUE if successful, otherwise FALSE is returned.
Note: This function assigns the new key name to the cells in the array. This will delete the original key name and not just reorder it.
Note: Since PHP 4.2.0, it is no longer necessary to sow the random number generator with the Srand () or Mt_srand () function, which is now automatically completed.
Grammar
Shuffle (array) parameter description
Array required. Specifies the array to use.
Example
Copy the Code code 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 number of elements to select. If more than one element is selected, the array containing the random key name is returned, otherwise the key name of the element is returned.
Note: If the specified Array_rand () function extracts more than 1 indexes, either the numeric index array or the associative array is extracted, the original array key is taken and placed in a new indexed array.
Note: Since PHP 4.2.0, it is no longer necessary to sow the random number generator with the Srand () or Mt_srand () function, which is now automatically completed.
Grammar
Array_rand (array,number) parameter description
Array required. Specifies the input array parameters.
Number is optional. The default is 1. Specifies how many random elements are returned.
Example 1
Copy the Code code as follows:
$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Print_r (Array_rand ($a, 1));
?>
Output:
B
Example 2
An array with a string key:
Copy the Code code as follows:
$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Print_r (Array_rand ($a, 2));
?>
Output:
Array ([0] = c [1] = b)
The above describes the PHP array function sequence of shuffle and array_rand random function introduction, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.