W3docs

Understanding the PHP Array Reverse Function

In PHP, the array_reverse() function is an in-built function that allows you to reverse the order of elements in an array. This function can be used to reverse

The PHP array_reverse() function returns a new array with the elements of the given array in the opposite order. It is a built-in function (no extension required) and works on both indexed and associative arrays. It is the go-to tool when you need a "last-to-first" view of data — for example, showing the newest comments first, walking a list backwards, or undoing a sort.

This page covers the syntax, how keys are handled, the $preserve_keys flag, and common pitfalls.

Syntax

array_reverse(array $array, bool $preserve_keys = false): array
ParameterRequiredDescription
$arrayYesThe array to reverse.
$preserve_keysNoIf true, numeric keys are kept attached to their values instead of being renumbered. String keys are always preserved, regardless of this flag. Defaults to false.

The function returns a new array; the original is never modified.

How Does the PHP Array Reverse Function Work?

array_reverse() walks the input array from the last element to the first and builds a new array in that order. By default, integer keys are renumbered starting from 0, while string keys are kept as-is. Set $preserve_keys to true when you need the original numeric keys to stay glued to their values.

Here is the simplest case — reversing an indexed array:

PHP example of array_reverse function usage

php— editable, runs on the server

Output:

Array
(
    [0] => cherry
    [1] => banana
    [2] => apple
)

As we can see, the order of elements in the $reversed_array is the reverse of the order of elements in the $original_array.

Using the PHP Array Reverse Function with Associative Arrays

As mentioned earlier, the array_reverse() function can also be used with associative arrays. When used with associative arrays, the order of elements is reversed, but the string keys remain unchanged by default.

Here is an example of how the array_reverse() function can be used with an associative array:

PHP example of how the array_reverse() function can be used with an associative array

php— editable, runs on the server

Output:

Array
(
    [c] => cherry
    [b] => banana
    [a] => apple
)

As we can see, the keys of the elements in the $reversed_array still correspond to the original keys, but the order of elements is reversed. String keys are always preserved, so the $preserve_keys flag makes no difference here.

Preserving Numeric Keys

For indexed (numeric-key) arrays, array_reverse() renumbers the keys from 0 by default. That is usually what you want, but sometimes the original index carries meaning — a line number, a position, an ID. Pass true as the second argument to keep those keys attached to their values:

PHP example of preserving numeric keys with array_reverse()

<?php

$data = array(10 => "ten", 20 => "twenty", 30 => "thirty");

// Default: numeric keys are renumbered from 0
print_r(array_reverse($data));

// preserve_keys = true: original numeric keys are kept
print_r(array_reverse($data, true));

?>

Output:

Array
(
    [0] => thirty
    [1] => twenty
    [2] => ten
)
Array
(
    [30] => thirty
    [20] => twenty
    [10] => ten
)

In the first array the values are reversed but the keys are reset; in the second the keys 30, 20, and 10 travel with their values.

Common Pitfalls

  • It does not modify the original array. array_reverse($arr) returns a new array — it does not reverse $arr in place. Always capture the return value: $arr = array_reverse($arr);.
  • It is not sorting. Reversing only flips the current order. To get descending order, sort first (e.g. with rsort() or arsort()) rather than reversing.
  • String keys ignore $preserve_keys. The flag only affects integer keys; string keys are kept either way.
  • Reversing a string. array_reverse() works on arrays only. To reverse a string, use strrev() instead.

Conclusion

In conclusion, the array_reverse() function is a powerful and versatile function that allows you to easily reverse the order of elements in an array. Whether you are working with indexed arrays or associative arrays, the array_reverse() function can help you to manipulate your arrays in the way that you need. With its simple syntax and straightforward behavior, the array_reverse() function is an essential tool for any PHP programmer working with arrays.

Practice

Practice
Which of the following is true about the array_reverse() function in PHP?
Which of the following is true about the array_reverse() function in PHP?
Was this page helpful?