PHP Tutorial - 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
    $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.