Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

shuffle

Shuffles the elements of an array into a random order.

Syntax

shuffle(array &$array): true

Parameters

ParameterTypeDescription
$arrayarrayThe array being shuffled.

Return value

trueAlways `true`; the array itself is modified.

Examples

A random order — different on every run:

<?php
 
$cards = [1, 2, 3, 4, 5];
 
shuffle($cards);
 
echo implode(', ', $cards);

The number and the set of elements stay the same:

<?php
 
$cards = [1, 2, 3, 4, 5];
 
shuffle($cards);
 
echo count($cards), ' ', array_sum($cards);

Output:

5 15

Notes

The keys are replaced with numbers from zero. array_rand() returns a single random key.

See also

Related recipes

In the tutorial

  • Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.

Thanks for reading!

Donate to the site — we work hard for you.