PHP array_reverse() Function

PHP array_reverse() function returns a new array with elements in reverse order.”

Syntax

array_reverse($array, $preserve)

Parameters

$array: The array parameter is required, and it describes the array.

$preserve: It is an optional parameter specifying whether the function should preserve the array’s keys. Possible values:

  1. true
  2. false(default)

Return value

It returns the reversed array.

Visual RepresentationVisual Representation of PHP array_reverse() Function

Example 1: How to Use array_reverse() Function

<?php
 
 $testArray = array(11, 21, 19, 46, 29);
 
 echo "Tested array: ";
 
 print_r($testArray);
 
 $reverseArray = array_reverse($testArray);
 
 echo "Reversed array: ";
 
 print_r($reverseArray);

Output

Tested array: Array
(
 [0] => 11
 [1] => 21
 [2] => 19
 [3] => 46
 [4] => 29
)
Reversed array: Array
(
 [0] => 29
 [1] => 46
 [2] => 19
 [3] => 21
 [4] => 11
)

In this example, the output shows the original array [11,21,19,46,29] reversed as [29,46,19,21,11].

Example 2: Passing preserve parameter as TrueVisual Representation of Passing preserve parameter as True

<?php

$testArray = array(11, 21, 19, 46, 29);

echo "Tested array: ";

print_r($testArray);

$reverseArray = array_reverse($testArray,true);

echo "Reversed array: ";

print_r($reverseArray);

Output

Tested array: Array
(
 [0] => 11
 [1] => 21
 [2] => 19
 [3] => 46
 [4] => 29
)
Reversed array: Array
(
 [4] => 29
 [3] => 46
 [2] => 19
 [1] => 21
 [0] => 11
)

In this example, the output array preserves the original keys, resulting in an array where the order of elements is reversed.

Example 3: Reverse Associative Array

<?php

$arr = array("Eleven"=>"Millie", "Mike"=>"Finn", "Dustin"=>"Gaten");

$reverseArray = array_reverse($arr);

print_r($reverseArray);

Output

Array
(
  [Dustin] => Gaten
  [Mike] => Finn
  [Eleven] => Millie
)

Example 4: Nested Array Reverse

<?php
 
 $testArray = array(
   'books' => array(
      array('name' => 'Game Of Thrones', 'Author' => 'George RR Martin'),
      array('name' => 'Hunger Games', 'Author' => 'Suzanne Collins'),
      array('name' => 'Harry Potter', 'Author' => 'JK Rowling')
   ),
   'movies' => array('Hunger Games', 'Harry Potter'));

 $reverseArray = array_reverse($testArray);
 print_r($reverseArray);

Output

Array
(
    [movies] => Array
        (
            [0] => Hunger Games
            [1] => Harry Potter
        )

    [books] => Array
        (
            [0] => Array
                (
                    [name] => Game Of Thrones
                    [Author] => George RR Martin
                )

            [1] => Array
                (
                    [name] => Hunger Games
                    [Author] => Suzanne Collins
                )

            [2] => Array
                (
                    [name] => Harry Potter
                    [Author] => JK Rowling
                )

        )

)

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.