shuffle
Shuffles the elements of an array into a random order.
Syntax
shuffle(array &$array): trueParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array | The 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 15Notes
The keys are replaced with numbers from zero. array_rand() returns a single random key.
See also
- random_int — Returns a random integer from a range.
- array_slice — Returns part of an array.
Related recipes
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.