PHP array_reverse() functiun

Instance

Return the array in reverse element order:

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

Run Instance

Definition and Usage

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

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 key names remain unchanged, otherwise the key names will be lost.

Syntax

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

Optional. Specifies whether to retain the original array key names.

This parameter was 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 while retaining the original array key names:

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