PHP 8 reference · Array Sorting Functions

PHP shuffle() Function

Shuffle an array into a random order, in place. Keys are discarded. Not suitable for security-sensitive randomness.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

shuffle(array &$array): true

Parameters

ParameterDescription
array The array to sort (changed in place).

Return value

Always true.

Examples

Shuffle a listTry it yourself
<?php
mt_srand(42); // fixed seed so the output is repeatable
$cards = ["A", "K", "Q", "J"];
shuffle($cards);
echo implode(" ", $cards), "\n";

Output

K A J Q
Random\RandomizerTry it yourself
<?php
$r = new Random\Randomizer(new Random\Engine\Mt19937(42));
echo implode(" ", $r->shuffleArray(["A", "K", "Q", "J"])), "\n"; // PHP 8.2+, returns a copy

Output

K A J Q

For secure shuffling (games with real prizes, lotteries) use (new Random\Randomizer())->shuffleArray($array), which uses a secure engine by default.

Version history

VersionChange
PHP 4.0Added
PHP 7.1Uses the Mt19937 generator, so mt_srand() seeds it.
PHP 8.2The return type is true (it was bool).

Was this page helpful?