PHP shuffle() Function
Example
Rearrange the elements of the array in a random order:
<?php $my_array = array("red","green","blue","yellow","purple"); shuffle($my_array); print_r($my_array); ?>
Definition and Usage
The shuffle() function rearranges the elements of the array in a random order.
This function assigns new key names to the elements of the array. Existing key names will be deleted (see the example below 1).
Syntax
shuffle(array)
Parameters | Description |
---|---|
array | Required. Specifies the array to be used. |
Technical Details
Return Value: | Returns TRUE on success, FALSE on failure. |
PHP Version: | 4+ |
Update Log: |
The random number generator will automatically seed since PHP 4.2.0. Note: This function assigns new key names to the elements of the array. This will delete the original key names, not just reorder them. Note: Since PHP 4.2.0, srand() or mt_srand() functions are no longer required to seed the random number generator, which is now automatically completed. |
More Examples
Example 1
Rearrange the elements of the array in a random order:
<?php $my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple"); shuffle($my_array); print_r($my_array); ?>