Shuffle () Definition and usage
The shuffle () function rearranges the elements in the array in random order.
If successful, returns TRUE, otherwise returns FALSE.
Note: This function assigns a new key name to the cell in the array. This deletes the original key name and not just the reordering.
Note: Since PHP 4.2.0, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.
Grammar
Shuffle (array) parameter description
Array required. Specify the array to use.
Example
Copy Code code 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 an array and returns.
The second parameter is used to determine how many 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, the key of the original array is taken, regardless of whether a numeric or associative array is drawn, and placed in a new indexed array.
Note: Since PHP 4.2.0, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.
Grammar
Array_rand (array,number) parameter description
Array required. Sets the input array parameters.
Number is optional. The default is 1. Specify how many random elements to return.
Example 1
Copy Code code as follows:
<?php
$a =array ("a" => "Dog", "B" => "Cat", "C" => "horse");
Print_r (Array_rand ($a, 1));
?>
Output:
B
Example 2
An array with a string key:
Copy Code code as follows:
<?php
$a =array ("a" => "Dog", "B" => "Cat", "C" => "horse");
Print_r (Array_rand ($a, 2));
?>
Output:
Array ([0] => C [1] => B)