Code Logo

Reverse Array with array_reverse()

Published at25 Jul 2026
PHP Collections Easy 0 views
Like0

Write a PHP function that takes an array and returns a new array with elements in reverse order using array_reverse().

array_reverse() is a PHP function that returns a new array with the order of elements reversed. Numeric keys are re-indexed by default, while string keys are preserved. An optional second parameter ($preserve_keys) can be set to true to preserve all keys.

This is the standard PHP way to reverse array order without writing manual loops. It is commonly used for reversing sorted results, implementing LIFO behavior, and processing data in reverse chronological order.

Time complexity is O(n) where n is the array length. The function creates a new array by copying elements in reverse order.

Example Input & Output

Example 1
Input
[1,2,3]
Output
[3,2,1]
Explanation

Simple reversal

Example 2
Input
[]
Output
[]
Explanation

Empty array

Example 3
Input
[10,20,30]
Output
[30,20,10]
Explanation

Three numbers reversed

Example 4
Input
[5]
Output
[5]
Explanation

Single element

Example 5
Input
['a','b','c']
Output
['c','b','a']
Explanation

String array

Algorithm Flow

Recommendation Algorithm Flow for Reverse Array with array_reverse()
Recommendation Algorithm Flow for Reverse Array with array_reverse()

Solution Approach

function solution($arr) {
    return array_reverse($arr);
}

Best Answers

php - Approach 1
<?php
function solution($arr) {
    return array_reverse($arr);
}