PHP shuffle() Function

Example

Reorder the elements of the array in random order:

<?php
$my_array = array("red","green","blue","yellow","purple");
shuffle($my_array);;
print_r($my_array);
?>

Run Instance

Definition and Usage

The shuffle() function reorders 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 the example below 1).

Syntax

shuffle(array)
Parameter Description
array Required. Specifies the array to be used.

Technical Details

Return Value: Returns TRUE if successful, FALSE if failed.
PHP Version: 4+
Update Log:

The random number generator will be automatically seeded starting from 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: Starting from PHP 4.2.0, srand() or mt_srand() function is no longer required to seed the random number generator, which is now automatically completed.

More Examples

Example 1

Reorder 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);
?>

Run Instance