PHP shuffle() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, shuffle() puts the elements of an array in a random order and gives them new keys 0, 1, 2 and so on.

It is useful for card games, quizzes and anything where the order should change each time. The array is changed in place. Because the result is random, a script that prints a shuffled array prints something different on each run.

Syntax

Call shuffle() with the values you need. Start with this line.

PHP
shuffle($cards);
CallWhat it does
shuffle($cards)Mixes the elements into a random order.
array_rand($cards)Picks a random key without changing the array.
sort($cards)Puts the shuffled values back in order.

shuffle() changes the original array and returns true. The old keys are removed, so the result is always a list with the keys 0 to n-1. The order is random, so you cannot predict it.

Library declarations
PHP
shuffle(array &$array): true

Parameters

ParameterDescription
arrayRequired. The array to shuffle. It is passed by reference and changed in place.

Return Value

Return ValueDescription
trueAlways returns true. The shuffled result is stored in the array you passed in.

PHP Examples

Example 1 — Keys Are Reset After Shuffling

The shuffled order is random, so this example prints only things that never change: the starting array, the new keys and the count.

PHP
<?php
$colors = ["x" => "red", "y" => "green", "z" => "blue"];

print_r($colors);

shuffle($colors);

echo "Keys after: ", implode(", ", array_keys($colors)), PHP_EOL;
echo "Count after: ", count($colors), PHP_EOL;

$copy = $colors;
sort($copy);
echo "Sorted copy: ", implode(", ", $copy), PHP_EOL;

How It Works

  1. print_r() shows the starting array with its string keys.
  2. shuffle() mixes the values and replaces the keys with 0, 1 and 2.
  3. The keys and the count are the same on every run, even though the order is random.
  4. Sorting a copy gives blue, green, red every time, which proves that no value was lost.

Example 2 — Check the Result Without Printing the Order

array_key_first() and array_key_last() always give the same answer after shuffle(), because the keys are renumbered.

PHP
<?php
$items = [10 => "a", 20 => "b", 30 => "c"];

shuffle($items);

var_dump(array_key_first($items));
var_dump(array_key_last($items));
echo gettype($items), PHP_EOL;

How It Works

  1. The starting array has the keys 10, 20 and 30.
  2. After shuffle() the keys are renumbered, so the first key is 0.
  3. The last key is 2 because there are three elements.
  4. gettype() confirms that the variable is still an array.

Example 3 — Shuffling Never Loses or Adds Values

After sorting a shuffled array we get the original list back. A one-element array stays the same.

PHP
<?php
$numbers = [1, 2, 3, 4, 5];
$original = $numbers;

shuffle($numbers);
echo "Count: ", count($numbers), PHP_EOL;
echo "Sum: ", array_sum($numbers), PHP_EOL;

sort($numbers);
var_dump($numbers === $original);

$single = ["only"];
shuffle($single);
echo $single[0], PHP_EOL;

How It Works

  1. A copy of the starting array is saved in $original.
  2. After shuffle() the count and the sum are the same, because the same values are still in the array.
  3. sort() puts the numbers back in order, and comparing with === shows that the array equals the original.
  4. An array with one element has only one possible order, so "only" is always printed.

Frequently Asked Questions

It randomly rearranges the elements of an array and renumbers the keys from 0.
No. All keys, including string keys, are replaced with 0, 1, 2 and so on. If you need the keys, shuffle the result of array_keys() and rebuild the array.
No. It changes the array you pass in and returns true.
Use array_rand($array) to get a random key, then read the value with $array[$key].
No. Use it for games and display order only. For passwords or tokens use random_int() or random_bytes().

Next: sizeof()

sizeof() returns the number of elements in an array. It is an alias of count().

sizeof() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful