Computer >> Computer tutorials >  >> Programming >> PHP

array_reverse() function in PHP


The array_reverse() function returns an array in the reverse order.

Syntax

array_reverse(arr, preservekey)

Parameters

  • arr− The specified array

  • preservekey− Possible values are TRUE and FALSE. Specifies preservation of keys of the array.

Return

The array_reverse() function returns the reversed array.

Example

The following is an example −

<?php
$arr = array("laptop", "mobile","tablet");
$reverse = array_reverse($arr);
print_r($arr);
print_r($reverse);
?>

Output

Array(
   [0] => laptop
   [1] => mobile
   [2] => tablet
)
Array (
   [0] => tablet
   [1] => mobile
   [2] => laptop
)

Example

Let us see another example −

<?php
$arr = array("mac", "windows","linux");
$reverse = array_reverse($arr, true);
print_r($arr);
print_r($reverse);
?>

Output

Array (
   [0] => mac
   [1] => windows
   [2] => linux
)
Array (
   [2] => linux
   [1] => windows
   [0] => mac
)