Backend Development 4 min read

How to Use PHP shuffle() to Randomly Rearrange Array Elements

This article explains the PHP shuffle() function, detailing its syntax, behavior of modifying the original indexed array, return value, and provides multiple code examples—including handling of non-indexed arrays—to demonstrate how to randomly reorder array elements in PHP.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Use PHP shuffle() to Randomly Rearrange Array Elements

In PHP programming, the shuffle() function is a very useful function that randomizes the order of elements in an array. This article introduces the specific usage of shuffle() and provides code examples to help readers understand and apply the function.

The syntax of the shuffle() function is:

shuffle(array &$array) : bool

The function accepts an array parameter $array and shuffles its elements in place, directly modifying the original array rather than returning a new one.

Below is a simple code example demonstrating how to use shuffle() :

// Declare and initialize an array
$myArray = array("Apple", "Banana", "Cherry", "Durian");

// Print the original array
echo "Original array: ";
print_r($myArray);

// Shuffle the array order
shuffle($myArray);

// Print the shuffled array
echo "Shuffled array: ";
print_r($myArray);

Running the above code produces output similar to:

Original array: Array
(
    [0] => Apple
    [1] => Banana
    [2] => Cherry
    [3] => Durian
)
Shuffled array: Array
(
    [0] => Durian
    [1] => Apple
    [2] => Banana
    [3] => Cherry
)

As shown, the original array elements are randomly reordered, and the variable $myArray itself is modified.

The shuffle() function also returns a boolean indicating whether the shuffle was successful. In the example above the return value is not used, but in real applications you may need to check it.

Note that shuffle() works only on indexed arrays (keys starting from 0 and consecutive). If the array has non‑sequential keys, shuffle() will reindex the array. The following example demonstrates this behavior with an associative array:

// Declare and initialize a non‑indexed array
$myArray = array("a" => "Apple", "b" => "Banana", "c" => "Cherry");

// Print the original array
echo "Original array: ";
print_r($myArray);

// Shuffle the array order
shuffle($myArray);

// Print the shuffled array
echo "Shuffled array: ";
print_r($myArray);

The output is:

Original array: Array
(
    [a] => Apple
    [b] => Banana
    [c] => Cherry
)
Shuffled array: Array
(
    [0] => Apple
    [1] => Banana
    [2] => Cherry
)

As shown, the keys of the non‑indexed array are reindexed to consecutive numbers after shuffling.

In summary, the shuffle() function is a practical PHP function for randomizing array element order, and with the examples provided readers can better understand and apply it in their own PHP code.

backendPHParrayshufflefunctioncoding
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.