Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

array_reverse()

Example

Return an array with its elements in reverse order.

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

Definition and Usage

The array_reverse() function returns an array with its elements reversed.

Syntax

array_reverse(array, preserve)

Parameter Values

 

Parameter

Description

array

Required. Specifies the array to be reversed.

preserve

Optional. Indicates whether the function should preserve the array keys. Possible values are:

  • true
  • false

Technical Details

Return Value:

Returns the array with elements in reverse order.

PHP Version:

4+

PHP Changelog:

The preserve parameter was introduced in PHP 4.0.3.

More Examples

Example

Return the original array, the reversed array, and the array with preserved 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);
?>