PHP array_reverse() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_reverse() returns a new array with the items in the opposite order.

By default numeric keys are renumbered, and you can pass true to keep them. String keys are always kept.

Syntax

Call array_reverse() with the values you need. Start with this line.

PHP
$reversed = array_reverse($items);
CallWhat it does
array_reverse([1, 2, 3])Returns [3, 2, 1].
array_reverse(["a", "b", "c"], true)Reverses the order but keeps the original keys 0, 1, 2.
array_reverse(["x" => 1, "y" => 2])Returns ["y" => 2, "x" => 1].

The original array is not changed. To reverse a string, you can combine array_reverse() with str_split() and implode(), or just use strrev().

Library declarations
PHP
array_reverse(array $array, bool $preserve_keys = false): array

Parameters

ParameterDescription
arrayRequired. The array to reverse.
preserve_keysOptional. If true, numeric keys are kept. Default is false, which renumbers them.

Return Value

Return ValueDescription
arrayA new array with the items in reverse order.

PHP Examples

Example 1 — Reverse a List

Flip a simple list of numbers.

PHP
<?php
$numbers = [1, 2, 3];

$reversed = array_reverse($numbers);

echo implode(", ", $reversed), PHP_EOL;

How It Works

  1. Create a list. It holds 1, 2 and 3.
  2. Call array_reverse(). It builds a new array with the last item first.
  3. Print the result. The order is 3, 2, 1.
  4. The original stays the same. $numbers is not changed.

Example 2 — Preserve Keys and String Keys

See how the second argument and string keys affect the result.

PHP
<?php
$letters = ["a", "b", "c"];

echo json_encode(array_reverse($letters)), PHP_EOL;
echo json_encode(array_reverse($letters, true)), PHP_EOL;
echo json_encode(array_reverse(["x" => 1, "y" => 2])), PHP_EOL;

How It Works

  1. Reverse normally. The keys are renumbered to 0, 1 and 2.
  2. Pass true. Each item keeps its old key, so c stays at key 2.
  3. Notice the JSON. Out-of-order keys make JSON show an object instead of a list.
  4. Reverse string keys. They are always kept with their values.

Example 3 — Reverse the Letters of a Word

Use str_split() and implode() with array_reverse().

PHP
<?php
$text = "PHP rocks";

$letters = str_split($text);
$reversed = array_reverse($letters);

echo implode("", $reversed), PHP_EOL;

How It Works

  1. Pick a string. It is PHP rocks.
  2. Split it into letters. str_split() makes an array of single characters.
  3. Reverse the array. The last letter now comes first.
  4. Join with an empty string. implode("", ...) builds the text again.

Frequently Asked Questions

It returns a new array with the items in reverse order. The original array is unchanged.
Pass true as the second argument: array_reverse($array, true).
It reverses their order, and the keys always stay with their values.
Use strrev($string). You can also use str_split(), array_reverse() and implode() together.
Assign the result back: $array = array_reverse($array).

Next: array_search()

array_search() looks for a value in an array and returns the key of the first match.

array_search() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful