0
0
PhpProgramBeginner · 2 min read

PHP Program to Reverse Array with Example Output

You can reverse an array in PHP using the built-in function array_reverse($array), which returns a new array with elements in reverse order.
📋

Examples

Input[1, 2, 3]
Output[3, 2, 1]
Input["apple", "banana", "cherry"]
Output["cherry", "banana", "apple"]
Input[]
Output[]
🧠

How to Think About It

To reverse an array, think of flipping the order of elements so the last becomes first and the first becomes last. You can do this by creating a new array and adding elements from the original array starting from the end to the beginning.
📐

Algorithm

1
Get the input array.
2
Create an empty array to hold reversed elements.
3
Start from the last element of the input array and move to the first.
4
Add each element to the new array in this reversed order.
5
Return or print the new reversed array.
💻

Code

php
<?php
// Original array
$array = [1, 2, 3, 4, 5];

// Reverse the array using built-in function
$reversed = array_reverse($array);

// Print the reversed array
print_r($reversed);
?>
Output
Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 )
🔍

Dry Run

Let's trace the example array [1, 2, 3, 4, 5] through the code.

1

Original array

The array is [1, 2, 3, 4, 5].

2

Call array_reverse

The function processes elements starting from 5 to 1.

3

Create reversed array

New array becomes [5, 4, 3, 2, 1].

4

Print reversed array

Output shows the reversed array.

IndexOriginal ValueReversed IndexReversed Value
0145
1234
2323
3412
4501
💡

Why This Works

Step 1: Using array_reverse

The array_reverse function takes the original array and returns a new array with elements in reverse order.

Step 2: No change to original array

The original array stays the same; the reversed array is a new copy.

Step 3: Printing the array

Using print_r shows the reversed array in a readable format.

🔄

Alternative Approaches

Manual loop reversal
php
<?php
$array = [1, 2, 3, 4, 5];
$reversed = [];
for ($i = count($array) - 1; $i >= 0; $i--) {
    $reversed[] = $array[$i];
}
print_r($reversed);
?>
This method manually builds the reversed array and helps understand the process but is longer than using the built-in function.
In-place swap reversal
php
<?php
$array = [1, 2, 3, 4, 5];
$length = count($array);
for ($i = 0; $i < $length / 2; $i++) {
    $temp = $array[$i];
    $array[$i] = $array[$length - 1 - $i];
    $array[$length - 1 - $i] = $temp;
}
print_r($array);
?>
This reverses the array in place without extra memory but is more complex to write.

Complexity: O(n) time, O(n) space

Time Complexity

Reversing an array requires visiting each element once, so the time is proportional to the number of elements, O(n).

Space Complexity

Using array_reverse creates a new array, so it uses O(n) extra space. The in-place swap method uses O(1) space.

Which Approach is Fastest?

The built-in array_reverse is optimized and easy to use, but in-place reversal saves memory at the cost of more complex code.

ApproachTimeSpaceBest For
array_reverse()O(n)O(n)Simplicity and readability
Manual loop reversalO(n)O(n)Learning how reversal works
In-place swap reversalO(n)O(1)Memory efficiency
💡
Use array_reverse for a quick and easy way to reverse arrays in PHP.
⚠️
Beginners often try to reverse arrays by looping forward instead of backward, which does not reverse the order.