PHP array_reverse() Function

Example

Return the array in the reverse order of elements:

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

Run Instances

Definition and Usage

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

Description

The array_reverse() function reverses the order of elements in the original array, creates a new array, and returns it.

If the second parameter is specified as true, the element keys remain unchanged; otherwise, the keys will be lost.

Syntax

array_reverse(array,preserve)
Parameter Description
array Required. Specifies the array.
preserve

Optional. Specifies whether to preserve the original array keys.

This parameter is added in PHP 4.0.3.

Possible Values:

  • true
  • false

Technical Details

Return Value: Return the reversed array.
PHP Version: 4+
Update Log: Added in PHP 4.0.3 preserve Parameters.

More Examples

Example 1

Return the original array, reverse the array, and return the reversed array with the original array keys:

<?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 Instances