PHP array_reverse()Functions

PHP Array ReferenceComplete PHP Array Reference

Examples

Returns an array in 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 reverse order.


Syntax

array_reverse(array,preserve)

Parameters Description
array Required. Specifies the array.
preserve Optional. Specifies whether to preserve the original array's keys.
If set to TRUE, numeric keys are preserved. Non-numeric keys are not affected by this setting and will always be preserved.
Possible values:
  • true
  • false

Technical Details

Return value: Returns the reversed array.
PHP Version: 4+
Changelog: preserveThe parameter was added in PHP 4.0.3.


More Examples

Example 1

Output the original array, the reversed array, and the reversed array with original keys preserved:

<?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 ReferenceComplete PHP Array Reference Other Extensions