Four Ways to Reverse an Array in PHP

This article demonstrates four methods to reverse a PHP array—including the built‑in array_reverse() function, a manual for‑loop swap, an array_reduce() technique, and a combination of array_multisort() with array_column()—explaining their usage, code examples, and output results.

php Courses
php Courses
php Courses
Four Ways to Reverse an Array in PHP

In PHP, arrays are a common data type that can store multiple values, and reversing an array is a frequent requirement.

Method 1: array_reverse() function

PHP provides the built‑in array_reverse() function which returns a new array with the elements in reverse order.

$arr = array(1, 2, 3, 4, 5);
$reversedArr = array_reverse($arr);
print_r($reversedArr);

The output is:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

The function can optionally preserve keys; the second parameter defaults to false. Setting it to true keeps the original keys.

$arr = array("one" => 1, "two" => 2, "three" => 3, "four" => 4, "five" => 5);
$reversedArr = array_reverse($arr, true);
print_r($reversedArr);

The output is:

Array
(
    [five] => 5
    [four] => 4
    [three] => 3
    [two] => 2
    [one] => 1
)

Method 2: for loop

A manual for loop can reverse an array by swapping elements from both ends using two pointers.

$arr = array(1, 2, 3, 4, 5);
$len = count($arr);
for ($i = 0; $i < $len / 2; $i++) {
    $temp = $arr[$i];
    $arr[$i] = $arr[$len - $i - 1];
    $arr[$len - $i - 1] = $temp;
}
print_r($arr);

The output is:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

Method 3: Using array_reduce() with reversed keys

The array_reduce() function can also be employed to reverse an array by iterating over its keys.

$arr = array(1, 2, 3, 4, 5);
$reversedArr = array_reduce(array_keys($arr), function ($acc, $key) use ($arr) {
    $acc[$key] = $arr[count($arr) - 1 - $key];
    return $acc;
}, []);
print_r($reversedArr);

The output is:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

This approach uses array_keys() to obtain indices and an accumulator $acc (initially an empty array) to build the reversed array. Although more complex, array_reduce() is useful for other array transformations.

Method 4: Using array_multisort() and array_column()

The final method leverages PHP's handling of multidimensional arrays. It separates odd and even indexed elements, reverses each with array_multisort(), and then recombines them using array_column().

$arr = array(1, 2, 3, 4, 5);
$n = count($arr);
for ($i = 0; $i < $n; $i++) {
    if ($i % 2 == 0) {
        $even[] = $arr[$i];
    } else {
        $odd[] = $arr[$i];
    }
}
array_multisort(array_reverse($odd), SORT_NUMERIC, array_reverse($even), SORT_NUMERIC);
$reversedArr = array_column(array($even, $odd), 0);
print_r($reversedArr);

The output is:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

Here, the odd and even elements are stored in $odd and $even arrays, respectively; array_multisort() reverses them, and array_column() flattens the result back to a single array.

In summary, the article covers four different techniques for reversing a PHP array: the simple array_reverse() function, a manual for loop, the more versatile array_reduce() method, and a multidimensional‑array approach using array_multisort() and array_column(). The built‑in function is the most straightforward, while the other methods illustrate alternative strategies and deeper PHP capabilities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPfor looparray_reversearray_reducearray_multisortarray_column
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

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.