PHP shuffle() function
Example
Rearrange the elements of the array in 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 random order.
This function assigns new key names to the elements of the array. Existing key names will be deleted (see Example 1 below).
Syntax
shuffle(array)
Parameter | 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 be automatically seeded 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 and 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 done automatically. |
More Examples
Example 1
Rearrange the elements of 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); ?>