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);
?>
Try it Yourself »

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 below).


Syntax

shuffle(array)

Parameter Values

Parameter Description
array Required. Specifies the array to use


Technical Details

Return Value: Returns TRUE on success or FALSE on failure
PHP Version: 4+
PHP Changelog:  PHP 4.2: The random number generator is seeded automatically

More Examples

Example

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);
?>
Try it Yourself »

❮ PHP Array Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.