THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP array_reverse() Function

PHP Array Reference 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));
?>
Run example »

Definition and Usage

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


Syntax

array_reverse(array,preserve)

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+
Changelog: The preserve parameter was added in PHP 4.0.3

More Examples

Example 1

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);
?>
Run example »

PHP Array Reference PHP Array Reference