PHP Function Reference

PHP shuffle() Function



The PHP shuffle() function shuffles (randomizes the order of the elements in) an array. It uses a pseudo random number generator to shuffle the elements of the array.

Syntax

shuffle(array)

Parameters

array Required. Specify the input array.

Return Value

Returns true on success or false on failure.

Exceptions

NA.

Example:

The example below shows the usage of shuffle() function.

<?php
$Arr = array(10, 20, 30, 40, 50, 60);

//displaying the array
print_r($Arr);

//shuffling the array
shuffle($Arr);

echo "\n";
//displaying the shuffled array
print_r($Arr);
?>

The output of the above code will be:

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
)

Array
(
    [0] => 50
    [1] => 20
    [2] => 40
    [3] => 30
    [4] => 10
    [5] => 60
)

❮ PHP Array Reference