array_reverse()

array_reverse() — Return an array with elements in reverse order.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Takes an input array and returns a new array with the order of the elements reversed.

Signature

array_reverse(array $array, bool $preserve_keys = false): array

Parameters

ParameterTypeWhat it takes
$arrayarrayThe input array.
$preserve_keysboolIf set to true numeric keys are preserved. Non-numeric keys are not affected by this setting and will always be preserved.

Return value

Returns the reversed array.

Example

A runnable snippet:

$input = array("php", 4.0, array("green", "red")); $reversed = array_reverse($input); $preserved = array_reverse($input, true); print_r($input); print_r($reversed); print_r($preserved);

Output:

Array ( [0] => php [1] => 4 [2] => Array ( [0] => green [1] => red ) ) Array ( [0] => Array ( [0] => green [1] => red ) [1] => 4 [2] => php ) Array ( [2] => Array ( [0] => green [1] => red ) [1] => 4 [0] => php )

See also

array_flip()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page array_reverse() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.