PHP array_reverse() Function

❮ PHP Array Reference

Example

Return an array in the reverse order:

<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>
Try it Yourself »

Definition and Usage

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


Syntax

array_reverse(array, preserve)

Parameter Values

Parameter Description
array Required. Specifies an array
preserve Optional. Specifies if the function should preserve the keys of the array or not. Possible values:
  • true
  • false


Technical Details

Return Value: Returns the reversed array
PHP Version: 4+
PHP Changelog: The preserve parameter was added in PHP 4.0.3

More Examples

Example

Return the original array, the reversed array and the preserved array:

<?php
$a=array("Volvo","XC90",array("BMW","Toyota"));
$reverse=array_reverse($a);
$preserve=array_reverse($a,true);

print_r($a);
print_r($reverse);
print_r($preserve);
?>
Try it Yourself »

❮ PHP Array Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.