The array_reverse is an inbuilt Function in PHP which returns the reverse of input single or multi-dimensional array. Also, according to the parameters, it can also preserve the original keys of the array. In this article, we will discuss the PHP array_reverse Function along with a few examples.
array array_reverse($array, $preserveKeys)
The PHP array_reverse Function expects two parameters. One of them is mandatory while the other is optional. The description of the parameters is given below:
- $array: The first parameter refers to the mandatory input array.
- $preserveKeys: The second parameter is optional. It whether to preserve original keys or not. It can take two values, true or false. By default the value of this parameter is false.
The function returns an array after reversing the input array.
Let’s discuss a few examples to demonstrate the use of PHP array_reverse Function.
Example 1: Single Dimensional Array
For instance, consider a single dimensional array and pass it to array_reverse function.
<?php $testArray = array(1, 2, 3, 4, 5); $reverseArray = array_reverse($testArray); print_r($reverseArray); /* Array (  => 5  => 4  => 3  => 2  => 1 ) */ ?>
The PHP array_reverse reverses the input array. Also, you can observe that it also resets the indices in the output array. The output array begins from 0
Example 2: Preserving Keys in Array
We can preserve the original keys of the input array by passing preserveKeys parameter as true. In the example below, the function preserves original array keys.
<?php $testArray = array(1, 2, 3, 4, 5); $reverseArray = array_reverse($testArray, true); print_r($reverseArray); /* Array (  => 5  => 4  => 3  => 2  => 1 ) */ ?>
Example 3: Associative Array
Similarly, we can also pass an associative array with string keys to array_reverse Function. However, the function always preserves original keys in case of string keys. This is shown in the example below:
<?php $testArray = array('a' => 'apple', 'b' => 'banana', 'c' => 'cat', 'd' => 'dog'); $reverseArray = array_reverse($testArray); print_r($reverseArray); /* Array ( [d] => dog [c] => cat [b] => banana [a] => apple ) */ ?>
Example 4: Nested Array
<?php $testArray = array( 'books' => array( array('name' => 'The Alchemist', 'Author' => 'Paula Hawkins'), 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); /* Array ( [movies] => Array (  => Hunger Games  => Harry Potter ) [books] => Array (  => Array ( [name] => The Alchemist [Author] => Paula Hawkins )  => Array ( [name] => Hunger Games [Author] => Suzanne Collins )  => Array ( [name] => Harry Potter [Author] => JK Rowling ) ) ) */ ?>
In the nested array above, notice that the function reverses the outer array. However, it does not reverse the inner keys of a multidimensional array as shown.
Vishesh is currently working as an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.