AppDividend
Latest Code Tutorials

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

0

PHP array_reverse() Function Example | How To Reverse Array In PHP is today’s topic. The array_reverse() function returns an array in the reverse order. We can use the PHP array_reverse() function to reverse the order of the array elements. It returns an array with items in reverse order.

PHP Array Reverse Example

The array_reverse() inbuilt function of PHP is used to reverse the items of the array, including the nested arrays.

Also, we have the option of preserving the key items according to the users choice.

An array_reverse() function accepts the array as a parameter and returns the array with items in reversed order.

The syntax of array_reverse() function is following.

array_reverse(array, preserve)

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

It is an optional parameter, and it specifies if the function should preserve the keys of the array or not. Possible values:

  1. true
  2. false

Single Dimensional Array Reverse In PHP

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

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

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

See the following code of Nested Array.

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

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

Finally, PHP array_reverse() Function Example | How To Reverse Array In PHP is over.

Recommended Posts

PHP ksort Function Example

PHP sort() Function Example

PHP strcmp() Example

PHP str_split Function Example

PHP str_repeat() Function Example

Leave A Reply

Your email address will not be published.

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