PHP array_reverse() Function – Returns an Array with Elements in Reverse Order
The article explains PHP’s array_reverse() function, detailing its syntax, parameters, return value, and provides example code demonstrating how to reverse an array with and without preserving keys, illustrating the resulting output for developers.
The array_reverse() function in PHP accepts an input array and returns a new array whose elements are in the opposite order.
Syntax
array_reverse(array $array, bool $preserve_keys = false)Parameters
array : The input array.
preserve_keys : When set to true , numeric keys are preserved; non‑numeric keys are always preserved.
Return value
Returns the reversed array.
Example
<?php
$input = array("php", 4.0, array("green", "red"));
$result = array_reverse($input);
$result_keyed = array_reverse($input, true);
?>When printed, $result yields:
Array
(
[0] => Array
(
[0] => green
)
[1] => 4
[2] => php
)And $result_keyed yields:
Array
(
[2] => Array
(
[0] => green
)
[1] => 4
[0] => php
)Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
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.