PHP array_reverse Function | Reverse Array

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.

Syntax

array array_reverse($array, $preserveKeys)

Parameters

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.

Return Value

The function returns an array after reversing the input array.

PHP array_reverse Function Diagram

Examples

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
	(
	    [0] => 5
	    [1] => 4
	    [2] => 3
	    [3] => 2
	    [4] => 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 index.

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
	(
	    [4] => 5
	    [3] => 4
	    [2] => 3
	    [1] => 2
	    [0] => 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
	        (
	            [0] => Hunger Games
	            [1] => Harry Potter
	        )
	    [books] => Array
	        (
	            [0] => Array
	                (
	                    [name] => The Alchemist
	                    [Author] => Paula Hawkins
	                )
	            [1] => Array
	                (
	                    [name] => Hunger Games
	                    [Author] => Suzanne Collins
	                )
	            [2] => 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.

Conclusion

In this article, we discussed the PHP array_reverse Function. You can learn about it on the Official Documentation of PHP. Also, you can read about more Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *