array_reverse

Returns a new array with the elements in reverse order, optionally keeping numeric keys.

Signature
array_reverse(array $array, bool $preserve_keys = false): array
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarrayThe input array. It is not modified.
$preserve_keysoptionalbool= falseWhen true, numeric keys stay attached to their values. String keys are always preserved regardless of this flag.

Return value

arrayA new array with the elements of $array in reverse order.

Errors

Throws a TypeError if $array is not an array.

array_reverse() returns a copy of the array with the order of the elements flipped. The original array is untouched, so you have to assign the result to something.

When to reach for it

Use it when you already have the data in the right order and just want it the other way round: showing the newest rows of a chronological log first, walking a breadcrumb trail from leaf to root, or reversing the result of a recursive build-up. It is the cheap alternative to re-sorting — if the array is already sorted ascending, reversing is faster and more obviously correct than running usort() with an inverted comparator.

What trips people up

By default numeric keys are renumbered from 0. So [10 => 'a', 20 => 'b'] becomes [0 => 'b', 1 => 'a'], silently destroying ids you were using as keys. Pass true as the second argument to keep them. String keys are never renumbered, which means a mixed-key array will keep half its keys and renumber the other half — usually a sign you should split the data instead.

It is also shallow: nested arrays inside are reversed only at the top level.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Show the most recent entries of an append-only activity log first without re-querying.
  • Turn a parent-to-child breadcrumb path into a child-to-parent path.
  • Reverse a chronologically sorted result set for a "newest first" toggle without a second sort.

Frequently asked questions

Does array_reverse modify the original array?

No. It returns a new array and leaves the input untouched, so you must assign the result. Functions that modify in place, like rsort(), take the array by reference instead.

Why did my array keys change after array_reverse?

Integer keys are renumbered from 0 by default. Call array_reverse($arr, true) to keep them; string keys are never renumbered.

What is the difference between array_reverse and rsort?

array_reverse flips the existing order without looking at values and returns a new array; rsort sorts the values in descending order and modifies the array in place, dropping the keys.

More examples

String keys survive either way

Both calls print the same thing: user, port, host with their original string keys. The preserve_keys flag only affects integer keys, so for a purely associative array it makes no difference.

Example · php
<?php
$config = ['host' => 'db1', 'port' => 5432, 'user' => 'app'];
print_r(array_reverse($config));
print_r(array_reverse($config, true));

Reversing only part of a list

Prints a, b, e, d, c. array_reverse has no offset parameter, so you slice out the portion you want, reverse that, and merge it back.

Example · php
<?php
$items = ['a', 'b', 'c', 'd', 'e'];
$head = array_slice($items, 0, 2);
$tail = array_reverse(array_slice($items, 2));
print_r(array_merge($head, $tail));

Reverse is shallow

The first call reverses the rows but leaves each inner pair as [1, 2]. Mapping array_reverse over the reversed outer array is what actually rotates the whole grid.

Example · php
<?php
$grid = [[1, 2], [3, 4], [5, 6]];
print_r(array_reverse($grid));

$deep = array_map('array_reverse', array_reverse($grid));
print_r($deep);

Discussion

  • Be the first to comment on this lesson.