Content · Programming · PHP

PHP array_reverse() Function: Usage and Examples

Understand array_reverse() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to read elements in the opposite order without sorting them. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The array_reverse() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is array_reverse() in PHP?

Read elements in the opposite order without sorting them.

Its syntax is:

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

Parameters used by this operation:

  • $array — The collection being examined or updated.
  • $preserve_keys — The value supplied for this argument affects the operation.

String keys are always preserved; numeric keys are renumbered unless preserve_keys is true. The input array is unchanged.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$steps = ["draft", "review", "publish"];
print_r(array_reverse($steps));

Output:

Array
(
    [0] => publish
    [1] => review
    [2] => draft
)

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$history = [10 => "opened", 20 => "edited", 30 => "saved"];
print_r(array_reverse($history, true));

Output:

Array
(
    [30] => saved
    [20] => edited
    [10] => opened
)

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Display a chronological event list newest first while retaining event identifiers

Display a chronological event list newest first while retaining event identifiers. Save the code in a PHP file and adapt the sample data to your project.

<?php

$activityLog = ["09:00 Login", "09:10 Upload", "09:30 Logout"];
$latestFirst = array_reverse($activityLog);
foreach ($latestFirst as $activity) { echo $activity . PHP_EOL; }

Output:

09:30 Logout
09:10 Upload
09:00 Login

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

String keys are always preserved; numeric keys are renumbered unless preserve_keys is true. The input array is unchanged.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
array_reverse()Read elements in the opposite order without sorting them
Related functionsarray_reverse() changes order; rsort() sorts values in descending order.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming input and result have identical keysCheck whether the function reindexes keys or retains original associations.
Confusing mutation with a returned copyString keys are always preserved; numeric keys are renumbered unless preserve_keys is true. The input array is unchanged.
Choosing a related function with different behaviorarray_reverse() changes order; rsort() sorts values in descending order.

Key Takeaways

  • array_reverse() helps you read elements in the opposite order without sorting them.
  • String keys are always preserved; numeric keys are renumbered unless preserve_keys is true. The input array is unchanged.
  • array_reverse() changes order; rsort() sorts values in descending order.
  • Use the practical example as a starting point, but validate real application inputs.

array_reverse() is useful when you need to read elements in the opposite order without sorting them. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.