How to reverse an array in PHP
In this tutorial, we are going to learn about how to reverse an array of elements in PHP.
Using array_reverse() method
To reverse an array in PHP, we can use the built-in array_reverse() function.
The array_reverse() function takes the array as an argument and returns the array of elements in reverse order.
Here is an example.
$fruits = array("oranges", "mangoes", "grapes");
$reverse = array_reverse($fruits);
print_r($reverse);
Output:
Array
(
[0] => grapes
[1] => mangoes
[2] => oranges
)
Note: The above array_reverse()
function doesn’t modifies the original array instead of it creates a new one with modified values.
Additional resources
You can also checkout the following tutorials for more knowledge: