Backend Development 5 min read

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

This article explains PHP's shuffle() function, its syntax, behavior on indexed and associative arrays, and provides code examples demonstrating how to randomize array elements and handle the function's boolean return value in practice.

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.

The syntax of the function is:

shuffle(array &$array) : bool

The function accepts an array parameter $array , shuffles its elements randomly, and modifies the original array directly instead of returning a new one.

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

// 声明并初始化一个数组
$myArray = array("Apple", "Banana", "Cherry", "Durian");

// 打印原始数组
echo "原始数组:";
print_r($myArray);

// 使用shuffle()函数打乱数组顺序
shuffle($myArray);

// 打印打乱后的数组
echo "打乱后的数组:";
print_r($myArray);

Running the above code produces output similar to:

原始数组:Array
(
    [0] => Apple
    [1] => Banana
    [2] => Cherry
    [3] => Durian
)
打乱后的数组:Array
(
    [0] => Durian
    [1] => Apple
    [2] => Banana
    [3] => Cherry
)

As shown, the shuffle() function randomly rearranges the elements and also modifies the value of $myArray .

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

Note that shuffle() works only on indexed arrays (arrays with consecutive numeric keys starting from 0). If the array keys are not consecutive, shuffle() will re‑index the array. The following example demonstrates this behavior with an associative array:

// 声明并初始化一个非索引数组
$myArray = array("a" => "Apple", "b" => "Banana", "c" => "Cherry");

// 打印原始数组
echo "原始数组:";
print_r($myArray);

// 使用shuffle()函数打乱数组顺序
shuffle($myArray);

// 打印打乱后的数组
echo "打乱后的数组:";
print_r($myArray);

The resulting output is:

原始数组:Array
(
    [a] => Apple
    [b] => Banana
    [c] => Cherry
)
打乱后的数组:Array
(
    [0] => Apple
    [1] => Banana
    [2] => Cherry
)

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

In summary, the shuffle() function is a practical tool in PHP for randomizing array element order, and understanding its behavior and return value helps developers use it effectively in their code.

PHP Practice

Scan the QR code to receive free learning materials

backendPHParrayrandomizationshufflecoding
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.