How to reverse the order of an array in PHP?

The array_reverse() function can be used to reverse the elements of an array.

The sample code is listed below:

<?php
$numbers = array(1, 2, 3, 4, 5);
$reversed = array_reverse($numbers);

print_r($reversed);
?>

Output:

Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

In the example above, we defined an array called $numbers containing numbers 1 to 5. We then used the array_reverse() function to reverse the order of the array elements and stored the result in a new array called $reversed. Finally, we used the print_r() function to display the reversed array.

bannerAds