Backend Development 2 min read

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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP array_reverse() Function – Returns an Array with Elements in Reverse Order

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
)
Backendphp-functionsarray-manipulationarray_reverse
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

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.