The array_reverse() function returns an array in the reverse order.
PHP array_reverse() Function has the following syntax.
array_reverse(array,preserve)
Parameter | Is Required | Description |
---|---|---|
array | Required. | Array to reverse |
preserve | Optional. | If to preserve the keys of the array or not. |
Possible values for preserve:
Reverse an associate array
<?php
$a=array("a"=>"A","b"=>"Java","c"=>"java2s.com");
print_r(array_reverse($a));
?>
The code above generates the following result.
Preserve the keys during the reverse
<?php
$a=array("Java","PHP",array("HTML","CSS"));
$reverse=array_reverse($a);
$preserve=array_reverse($a,true);
print_r($a);
print_r($reverse);
print_r($preserve);
?>
The code above generates the following result.