How to Randomize the Order Array Elements in PHP
John Mwaniki / Updated on 07 Jul 2024After creating an array, there may be scenarios where you would want to randomly reorganize the order in which elements appear in it.
For instance, let's say you have a quiz application. You would want to present the questions in a randomized order to prevent users from memorizing the sequence, thus encouraging a genuine understanding of the material.
Another example is in data sampling and analysis. If you need to analyze a subset of the data, shuffling will ensure a random, unbiased sample, protecting your results from skewed perspectives.
Random Reordering Array Elements in PHP
PHP has an in-built, simple, and effective way to randomize array elements using the shuffle()
function.
Syntax
shuffle(array)
This function accepts an array as a parameter, randomizes the order of the elements in the array, and returns true
on success or false
on failure.
Example 1: Shuffling a Numeric Array
<?php
$animals = array("Lion", "Zebra", "Rhino", "Warthog");
echo "Original array:<br>";
print_r($animals);
// Array shuffling
shuffle($animals);
echo "<br>Shuffled array:<br>";
print_r($animals);
Output:
Original array:
Array ( [0] => Lion [1] => Zebra [2] => Rhino [3] => Warthog )
Shuffled array:
Array ( [0] => Rhino [1] => Warthog [2] => Lion [3] => Zebra )
As you can see, the elements have been randomly reordered. They also have different indexes from the ones they had earlier.
It is important to note that this function assigns new keys to the elements in the array. Rather than just reordering the keys, existing keys are removed and the array's internal pointer reset to the first element.
Example 2: Shuffling an Associative Array
<?php
$student = array("name" => "James", "age" => 30, "height" => "6 ft", "gender" => "Male");
echo "Original array:<br>";
print_r($student);
// Array shuffling
shuffle($student);
echo "<br>Shuffled array:<br>";
print_r($student);
Output:
Original array:
Array ( [name] => James [age] => 30 [height] => 6 ft [gender] => Male )
Shuffled array:
Array ( [0] => 30 [1] => Male [2] => 6 ft [3] => James )
As you can notice in the above example, the order of elements have been changed. However, we have also lost the keys, which have been removed and replaced by numeric indexes.
The array no longer holds its original meaning where we would simply use the known keys to access the relevant values.
To change the order of elements in an associative array using the shuffle()
function without losing the keys, we can do it as below:
<?php
$student = array("name" => "James", "age" => 30, "height" => "6 ft", "gender" => "Male");
echo "Original array:<br>";
print_r($student);
// Getting array keys
$keys = array_keys($student);
// Shuffling array keys
shuffle($keys);
$shuffledArray = array();
foreach ($keys as $key) {
$shuffledArray[$key] = $student[$key];
}
echo "<br>Shuffled array:<br>";
print_r($shuffledArray);
Output:
Original array:
Array ( [name] => James [age] => 30 [height] => 6 ft [gender] => Male )
Shuffled array:
Array ( [age] => 30 [gender] => Male [height] => 6 ft [name] => James )
In the above example, we have used the array_keys()
function to obtain an array of keys, shuffled them, and then reconstructed the array with the shuffled keys.
You can also use the method above to randomize the order of elements in a numeric array together with their indexes.
<?php
$colors = array("Brown", "Yellow", "Green", "Red", "Blue");
echo "Original array:<br>";
print_r($colors);
// Getting array indexes
$indexes = array_keys($colors);
// Shuffling array indexes
shuffle($indexes);
$shuffledColors = array();
foreach ($indexes as $index) {
$shuffledColors[$index] = $colors[$index];
}
echo "<br>Shuffled array:<br>";
print_r($shuffledColors);
Output:
Original array:
Array ( [0] => Brown [1] => Yellow [2] => Green [3] => Red [4] => Blue )
Shuffled array:
Array ( [4] => Blue [3] => Red [1] => Yellow [0] => Brown [2] => Green )
As you can see above, the order has changed but each element has retained its original index. In case you want to reorder an array without assigning new keys to its elements, then this is the method to use in such as scenario.
That's it!
By going through this article, I hope you can now easily and effectively randomize the order of elements in a PHP array.