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.
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.
<code>$arr = array(1, 2, 3, 4, 5);
$reversedArr = array_reverse($arr);
print_r($reversedArr);
</code>The output is:
<code>Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
</code>The function can optionally preserve keys; the second parameter defaults to false . Setting it to true keeps the original keys.
<code>$arr = array("one" => 1, "two" => 2, "three" => 3, "four" => 4, "five" => 5);
$reversedArr = array_reverse($arr, true);
print_r($reversedArr);
</code>The output is:
<code>Array
(
[five] => 5
[four] => 4
[three] => 3
[two] => 2
[one] => 1
)
</code>Method 2: for loop
A manual for loop can reverse an array by swapping elements from both ends using two pointers.
<code>$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);
</code>The output is:
<code>Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
</code>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.
<code>$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);
</code>The output is:
<code>Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
</code>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() .
<code>$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);
</code>The output is:
<code>Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
</code>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.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.