How to Reverse an Array in PHP?
Last Updated :
18 Jun, 2024
Reversing an array in PHP involves rearranging its elements so that the last element becomes the first, the second-to-last becomes the second, and so on. This operation is essential in various programming scenarios for altering data order or preparing data for specific operations. Implementing array reversal efficiently ensures that data manipulation tasks are executed smoothly and effectively.
Example:
Input: $array = [1,2,3,4,5];
Output: Array ( [0] => 5[1] => 4[2] => 3[3] => 2[4] => 1 )
There are several approaches to reverse an array in PHP hp which are as follows:
Using the array_reverse() function
We are going to use the $reversedArray() function to reverse the given array. Then, we have use the print_r to print the output of the same.
Example: This example shows the reverse of an array using the array_reverse() function.
PHP
<?php
$originalArray = array(1, 2, 3, 4, 5);
echo "Original Array: ";
print_r($originalArray);
echo "\n";
$reversedArray = array_reverse($originalArray);
echo "Reversed Array: ";
print_r($reversedArray);
echo "\n";
?>
OutputOriginal Array: Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Reversed Array: Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
Using a for loop
We are going to reverse an array using a loop in which we will first initialize an empty array and store the reversed elements. Then we will iterate it through the original array starting from the last element and moving towards the first element using for loop in which each iteration will append the current element to the new array. At last, the loop ends and the new array will contain the elements in reverse order.
Example: This example shows the reverse of an array using the for loop.
PHP
<?php
$originalArray = array(1, 2, 3, 4, 5);
$reversedArray = array();
$length = count($originalArray);
for ($i = $length - 1; $i >= 0; $i--) {
$reversedArray[] = $originalArray[$i];
}
echo "Original Array: ";
print_r($originalArray);
echo "\nReversed Array: ";
print_r($reversedArray);
?>
OutputOriginal Array: Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Reversed Array: Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
Using the array_reverse() function with array_keys() and array_combine()
We are going to reverse an array using the array_reverse() function along with array_keys() and array_combine(). We use the array_keys() to get the keys of the original array and then use array_reverse() to reverse the order of the keys. At last, we use array_combine() to combine the reversed keys with the original array values and print_r to print the output.
Example: This example shows the reverse of an array Using the array_reverse() function with array_keys() and array_combine().
PHP
<?php
$originalArray = array(
'a' => 'Apple',
'b' => 'Banana',
'c' => 'Cherry',
'd' => 'Date'
);
$keys = array_keys($originalArray);
$reversedKeys = array_reverse($keys);
$reversedArray = array_combine($reversedKeys, $originalArray);
echo "Original Array:<br>";
print_r($originalArray);
echo "<br><br>Reversed Array:<br>";
print_r($reversedArray);
?>
OutputOriginal Array:<br>Array
(
[a] => Apple
[b] => Banana
[c] => Cherry
[d] => Date
)
<br><br>Reversed Array:<br>Array
(
[d] => Apple
[c] => Banana
[b] => Cherry
[a] => Dat...
Similar Reads
How to reset Array in PHP ? You can reset array values or clear the values very easily in PHP. There are two methods to reset the array which are discussed further in this article. Methods: unset() Functionarray_diff() Function Method 1: unset() function: The unset() function is used to unset a specified variable or entire arr
2 min read
How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he
2 min read
Reverse an Array in Java Reversing an Array is a common task in every programming language. In Java, there are multiple ways to reverse an array. We can reverse it manually or by using built-in Java methods. In this article, we will discuss different methods to reverse an array with examples.Let us first see the most common
4 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read
Perl | Reverse an array Reverse an array or string in Perl. Iterative Way: Iterate over the array from 0 to mid of array. Swap the arr[i] element with arr[size-i] element. Perl #Perl code to reverse an array iteratively #declaring an array of integers @arr = (2, 3, 4, 5, 6, 7); # Store length on array in $n variable $n = $
2 min read
Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3
3 min read
How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte
3 min read
How to display array structure and values in PHP ? In this article, we will discuss how to display the array structure and values in PHP. To display the array structure and its values, we can use var_dump() and print_r() functions. It includes Array sizeArray valuesArray value with IndexEach value data type We will display the array structure using
2 min read
PHP Change strings in an array to uppercase Changing strings in an array to uppercase means converting all the string elements within the array to their uppercase equivalents. This transformation modifies the array so that every string, regardless of its original case, becomes fully capitalized.Examples:Input : arr[] = ("geeks", "For", "GEEks
3 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read