PHP shuffle()Functions

PHP Array ReferenceComplete PHP Array Reference Manual

Examples

Rearrange the elements in an array in random order:

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

shuffle($my_array);
print_r($my_array);
?>

Run Example »

Definition and Usage

The shuffle() function rearranges the elements in an array in random order.

This function assigns new key names to the elements in the array, and existing key names will be removed (see Example 1 below).


Syntax

shuffle(array)

Parameter Description
array Required. Specifies the array to use.

Technical Details

Return value: Returns TRUE on success, or FALSE on failure.
PHP Version: 4+
Changelog: As of PHP 4.2.0, the random number generator is seeded automatically.


More Examples

Example 1

Rearrange the elements in an 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 Example »


PHP Array ReferenceComplete PHP Array Reference Manual Other Extensions