Home > Article > Backend Development > How to use php shuffle function
php shuffle function is used to rearrange the elements in the array in random order. The syntax is shuffle(array), and the parameter array is required and refers to the array to be used.
#How to use the php shuffle function?
Definition and usage
shuffle() function rearranges the elements in the array in random order.
This function assigns new key names to elements in the array, and existing key names will be deleted (see Example 1 below).
Syntax
shuffle(array)
Parameters
array Required. Specifies the array to use.
Return value: TRUE if successful, FALSE if failed.
PHP Version: 4
Change Log: As of PHP 4.2.0, the random number generator is automatically seeded.
Example 1
Rearrange the elements in the array in random order:
<?php $my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple"); shuffle($my_array); print_r($my_array); ?>
Output:
Array ( [0] => red [1] => blue [2] => green [3] => purple [4] => yellow )
Example 2
Rearrange the elements in the array in random order:
<?php $my_array = array("red","green","blue","yellow","purple"); shuffle($my_array); print_r($my_array); ?>
Output:
Array ( [0] => blue [1] => green [2] => yellow [3] => red [4] => purple )
The above is the detailed content of How to use php shuffle function. For more information, please follow other related articles on the PHP Chinese website!