Latest web development tutorials
 

PHP shuffle() Function

< PHP Array Reference

Example

Randomize the order of the elements in the array:

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

shuffle($my_array);
print_r($my_array);
?>
Run example »

Definition and Usage

The shuffle() function randomizes the order of the elements in the array.

This function assigns new keys for the elements in the array. Existing keys 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

Randomize the order of the elements in the array:

<?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 Reference