Signature
array_reverse(array $array, bool $preserve_keys = false): arrayPurpose
array_reverse() takes an array and gives you a new array with the same elements in the opposite order. The last element comes first and the first element comes last. It is useful when you store items oldest-first but want to show them newest-first, or when you want to walk a list backwards without writing an index loop.
The function does not change the array you pass in. It builds and returns a fresh array, so you must use the return value.
Signature
Parameters and defaults
$arrayis the input array. Any array works: lists, associative arrays, mixed keys and nested arrays.$preserve_keysdefaults tofalse. When it isfalse, numeric keys are renumbered from0in the new order. When it istrue, numeric keys stay attached to their original values. String keys are always preserved, whatever you pass for this flag.
Return value and failure cases
The function returns the reversed array. An empty array gives back an empty array. There is no false return value to check for.
Because the parameter is typed array, passing something else such as a string or null throws a TypeError. If you need to reverse a string, that is a different job for a string function.
Example 1: reversing a list
With a simple list, the result is renumbered from zero and the original variable is untouched.
$letters = ['a', 'b', 'c', 'd'];
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
echo json_encode(array_reverse($letters), $flags), "\n";
echo json_encode($letters, $flags), "\n";["d","c","b","a"] ["a","b","c","d"]
The first line shows the reversed copy and the second shows that $letters still has its original order.
Example 2: preserving numeric keys
Numeric keys are often meaningful, such as IDs. Pass true as the second argument to keep them.
$items = [10 => 'ten', 20 => 'twenty', 30 => 'thirty'];
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
echo json_encode(array_reverse($items), $flags), "\n";
echo json_encode(array_reverse($items, true), $flags), "\n";["thirty","twenty","ten"]
{"30":"thirty","20":"twenty","10":"ten"}
Without the flag, the IDs 10, 20 and 30 are lost and the values become a plain list. With the flag, each value keeps its ID, so the encoded JSON is an object keyed by those numbers.
Example 3: mixed string and numeric keys
The flag only concerns numeric keys. String keys survive either way.
$data = ['x' => 1, 5 => 'five', 'y' => 2, 9 => 'nine'];
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
echo json_encode(array_reverse($data), $flags), "\n";
echo json_encode(array_reverse($data, true), $flags), "\n";{"0":"nine","y":2,"1":"five","x":1}
{"9":"nine","y":2,"5":"five","x":1}
In the default result, nine becomes key 0 and five becomes key 1, while x and y keep their names. With true, keys 9 and 5 are kept too.
Edge cases
- Empty arrays: the result is an empty array.
- Single element: the result has the same single element. With the default flag, a numeric key becomes
0. - Nested arrays: only the top level is reversed. An inner array keeps its own order, so reverse it separately if you need that.
- Preserved keys on a list: reversing
[1, 2, 3]withtruegives keys2,1,0. That array is no longer a sequential list, andjson_encode()will write it as an object instead of a JSON array. - Values are not changed: only positions move. Types and nested values stay as they were.
Common mistakes
- Ignoring the return value. Writing
array_reverse($list);on its own line does nothing useful. Assign the result:$list = array_reverse($list);. - Expecting numeric keys to survive by default. If keys carry meaning, pass
true. - Assuming
trueaffects string keys. It does not. String keys are always kept. - Confusing reversing with sorting.
array_reverse()flips the current order; it does not compare values. Use a sorting function such asrsort()orkrsort()for descending order by value or key. - Passing a non-array. A string or
nullcauses aTypeError. Make sure the input is an array first. - Reindexing by accident. After reversing with preserved keys, a loop that relies on
$array[0]may fail. Usearray_values()if you need a fresh list.
Related functions
array_flip()exchanges keys and values. It is listed as a related function in the manual, but it swaps keys with values rather than changing order.array_values()renumbers an array as a list.rsort()andkrsort()sort in descending order.array_slice()takes a portion of an array and can be combined with reversing.
Recap
array_reverse() returns a new array with elements in reverse order and leaves the original alone. Numeric keys are renumbered unless $preserve_keys is true, string keys are always kept, nested arrays are not reversed deeply, and non-array input throws a TypeError.
Tested examples
$letters = ['a', 'b', 'c', 'd'];
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
echo json_encode(array_reverse($letters), $flags), "\n";
echo json_encode($letters, $flags), "\n";
["d","c","b","a"] ["a","b","c","d"]
$items = [10 => 'ten', 20 => 'twenty', 30 => 'thirty'];
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
echo json_encode(array_reverse($items), $flags), "\n";
echo json_encode(array_reverse($items, true), $flags), "\n";
["thirty","twenty","ten"]
{"30":"thirty","20":"twenty","10":"ten"}
$data = ['x' => 1, 5 => 'five', 'y' => 2, 9 => 'nine'];
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
echo json_encode(array_reverse($data), $flags), "\n";
echo json_encode(array_reverse($data, true), $flags), "\n";
{"0":"nine","y":2,"1":"five","x":1}
{"9":"nine","y":2,"5":"five","x":1}