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

