PHP array_reverse() Function

Definition

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

Syntax

PHP array_reverse() Function has the following syntax.

array_reverse(array,preserve)

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to reverse
preserveOptional.If to preserve the keys of the array or not.

Possible values for preserve:

  • true
  • false

Example 1

Reverse an associate array


<?php
    $a=array("a"=>"A","b"=>"Java","c"=>"java2s.com");
    print_r(array_reverse($a));
?>

The code above generates the following result.

Example 2

Preserve the keys during the reverse


<?php/*from w ww  .java2  s  . com*/
    $a=array("Java","PHP",array("HTML","CSS"));
    $reverse=array_reverse($a);
    $preserve=array_reverse($a,true);
    
    print_r($a);
    print_r($reverse);
    print_r($preserve);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions