Home >Backend Development >PHP Tutorial >PHP array function sequence shuffle and array_rand introduction to the use of random functions
shuffle() definition and usage
shuffle() function rearranges the elements in the array in random order.
If successful, return TRUE, otherwise return FALSE.
Note: This function assigns a new key name to the unit in the array. This will delete the original keys rather than just reorder them.
Note: As of PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() functions, it is now done automatically.
Syntax
shuffle(array) Parameter Description
array Required. Specifies the array to use.
Example
Copy code The code is as follows:
$my_array = array("a" => "Dog", "b" => "Cat", "c" = > "Horse");
shuffle($my_array); print_r($my_array);
?>
Copy the code The code is as follows:
$a=array("a"=>"Dog","b"=>"Cat","c" =>"Horse");
print_r(array_rand($a,1));
?>
Copy code The code is as follows:
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r (array_rand($a,2));
?>
The above is an introduction to the use of shuffle and array_rand random functions in PHP array function sequence, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.