Home >>PHP Array Functions >PHP array_reverse() Function

PHP array_reverse() Function

PHP array_reverse() Function

PHP array_reverse() function is used to reverse the elements of a given input array including the nested arrays. It accepts an array as input parameter. It returns the array passed to it as parameter with elements in reversed order.

Syntax:

array_reverse($array, $preserve);

Parameter Values

Parameter Description
array This is a required parameter. This parameter defines the given array.
preserve This is an optional parameter. This parameter defines if the function should preserve the keys of the array or not.

Here is an example of array_reverse() function in PHP:

<html>
<body>
<pre>
<?php
$a=array("u","y","n","a","m","i","h","b","A");
print_r(array_reverse($a));
?>
</pre>
</body>
</html>
Output:
Array
(
    [0] => A
    [1] => b
    [2] => h
    [3] => i
    [4] => m
    [5] => a
    [6] => n
    [7] => y
    [8] => u
)

Example 2:

<html>
<body>
<pre>
<?php
$a=array("Jerry","PHP","Developer","Noida");
print_r(array_reverse($a));
echo "<br>";
print_r(array_reverse($a, true));
?>
</pre>
</body>
</html>
Output:
Array
(
    [0] => Noida
    [1] => Developer
    [2] => PHP
    [3] => Jerry
)

Array
(
    [3] => Noida
    [2] => Developer
    [1] => PHP
    [0] => Jerry
)

No Sidebar ads