The array_reverse function reverses the elements of the given array and returns them. For instance, the first element of the array will appear at the last and the last will appear at the first applying this transition to the elements in between.
What is the syntax of the array_reverse function in PHP?
array_reverse(array, preserve)
Parameters | Details |
---|---|
array | The array to reverse the elements of – Required |
preserve | Boolean parameter – When set to true: Preserve the keys of the array while reversing. When set to false: keys are not preserved while reversing – Optional |
Example of array_reverse function
<?php
$arr=array("Suzuki","CD70",array("Honda","Toyota"));
$arr_reversed=array_reverse($arr);
$arr_reversed_preserved=array_reverse($arr,true);
print_r($arr);
print_r($arr_reverse);
print_r($arr_reversed_preserved);
?>
In the above example, we print the original array, reversed array, and the reversed array with preserved parameter set to true. Run this script to have the best understanding of the array_reverse function.