PHP array_reverse: How to reverse array in PHP

0
35
PHP array_reverse() Function Example | How To Reverse Array In PHP

The array_reverse() function returns an array in the reverse order. We can use the array_reverse() function to reverse the order of the array elements. It returns an array with items in reverse order.

PHP array_reverse

The array_reverse() is a built-in PHP function used to reverse the elements of an array, including the nested arrays. The array_reverse() function accepts the array as a parameter and returns the array with items in reversed order.

We can preserve the key elements according to the user’s choice.

Syntax

The syntax of array_reverse() function is following.

array_reverse(array, preserve)

Parameters

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

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

  1. true
  2. false

Single Dimensional Array Reverse in PHP

To reverse a single-dimensional array in PHP, use the array_reverse() function.

See the following code.

<?php
  
  $testArray = array(11, 21, 19, 46, 29);
  $reverseArray = array_reverse($testArray);
  print_r($reverseArray);

See the output.

➜  pro php app.php
Array
(
    [0] => 29
    [1] => 46
    [2] => 19
    [3] => 21
    [4] => 11
)
➜  pro

Associative Array Reverse in PHP

To reverse an associative array in PHP, use the array_reverse() function.

See the following code example.

<?php

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

See the output.

➜  pro php app.php
Array
(
    [Dustin] => Gaten
    [Mike] => Finn
    [Eleven] => Millie
)
➜  pro

The next program reverses an array taking the $key_preserve as FALSE by default. This doesn’t preserve the keys.

<?php
  
$arr = array("Netflix"=>"Marianne", 
            "Movie"=>"Valek", 
            "Bollywood"=>"Daayan");
$rev = array_reverse($arr, FALSE);
print_r($rev);

See the output.

➜  pro php app.php
Array
(
    [Bollywood] => Daayan
    [Movie] => Valek
    [Netflix] => Marianne
)
➜  pro

Nested Array Reverse In PHP

To reverse a nested array in PHP, use the array_reverse() function.

<?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);

See the output.

➜  pro php app.php
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
                )

        )

)
➜  pro

Notice that a function reverses an outer array in the nested array above. However, it does not reverse the inner keys of the multidimensional array, as shown.

That’s it for this tutorial.

Recommended Posts

PHP ksort

PHP sort

PHP strcmp

PHP str_split

PHP str_repeat

Leave A Reply

Please enter your comment!
Please enter your name here

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