In this tutorial we will learn How to reverse an array in PHP, we can use PHP array_reverse() function which is a built-in php function made for this purpose. This function can reverse any array passed to it.
Table of Contents
PHP array_reverse() function
PHP array_reverse() function returns an array which is a reversed version of original array.
array_reverse() function can also preserve the keys if we want to.
Reversing an Indexed array
<?php
$array = array("HTML","CSS","PHP");
$reversedArray = array_reverse($array);
print_r($reversedArray);
?>
The above code shows how to reverse an indexed array with help of array_reverse() function.
Reversing an Associative array
<?php
$array = array("a"=>"HTML","b"=>"CSS","c"=>"PHP");
$reversedArray = array_reverse($array);
print_r($reversedArray);
?>
Second example shows how to reverse an associative array.