PHP | array_reverse() Function - GeeksforGeeks

09 Mar, 2018
This inbuilt function of PHP is used to reverse the elements of an array including the nested arrays. Also, we have an option of preserving the key elements according to the users choice. This function accepts an array as parameter and returns the array with elements in reversed order.
Syntax:
array array_reverse($array, $key_preserve)
Parameters:The function takes two arguments and are described below:
$array (mandatory):This parameter refers to the original array.$key_preserve (optional): This is an optional parameter and can be set to TRUE or FALSE, it refers to the preservation of keys of the array. By default the value of this parameter is taken as FALSE.
$array (mandatory):This parameter refers to the original array.
$key_preserve (optional): This is an optional parameter and can be set to TRUE or FALSE, it refers to the preservation of keys of the array. By default the value of this parameter is taken as FALSE.
Return Value:This function returns the array passed in parameter with elements in reversed order.
Examples:
Input : $array = (2, 4, 5, 10, 100)
Output : 
Array
(
    [0] => 100
    [1] => 10
    [2] => 5
    [3] => 4
    [4] => 2
)

Input :
Array
(
    [0] => ram
    [1] => aakash
    [2] => saran
    [3] => mohan
)
Output :
Array
(
    [3] => mohan
    [2] => saran
    [1] => aakash
    [0] => ram
)

Below programs illustrate the array_reverse() function in PHP:
This program reverses an array taking the $key_preserve as FALSE by default. This don’t presere the keys.<?php  // PHP function to illustrate the use of array_reverse()function Reverse($array){    return(array_reverse($array));}  $array = array("ram", "aakash", "saran", "mohan");  echo "Before:\n";print_r($array);  echo "\nAfter:\n";print_r(Reverse($array));  ?>Output:Before:
Array
(
    [0] => ram
    [1] => aakash
    [2] => saran
    [3] => mohan
)

After:
Array
(
    [0] => mohan
    [1] => saran
    [2] => aakash
    [3] => ram
)Let’s see what happens when we pass the key_preserve parameter as TRUE. This preserve the keys.<?php  // PHP function to illustrate the use of array_reverse()function Reverse($array){    return(array_reverse($array, true));}  $array = array("ram", "aakash", "saran", "mohan");  echo "Before:\n";print_r($array);  echo "\nAfter:\n";print_r(Reverse($array));  ?>Output:Before:
Array
(
    [0] => ram
    [1] => aakash
    [2] => saran
    [3] => mohan
)

After:
Array
(
    [3] => mohan
    [2] => saran
    [1] => aakash
    [0] => ram
)

This program reverses an array taking the $key_preserve as FALSE by default. This don’t presere the keys.<?php  // PHP function to illustrate the use of array_reverse()function Reverse($array){    return(array_reverse($array));}  $array = array("ram", "aakash", "saran", "mohan");  echo "Before:\n";print_r($array);  echo "\nAfter:\n";print_r(Reverse($array));  ?>Output:Before:
Array
(
    [0] => ram
    [1] => aakash
    [2] => saran
    [3] => mohan
)

After:
Array
(
    [0] => mohan
    [1] => saran
    [2] => aakash
    [3] => ram
)
<?php  // PHP function to illustrate the use of array_reverse()function Reverse($array){    return(array_reverse($array));}  $array = array("ram", "aakash", "saran", "mohan");  echo "Before:\n";print_r($array);  echo "\nAfter:\n";print_r(Reverse($array));  ?>
Output:
Before:
Array
(
    [0] => ram
    [1] => aakash
    [2] => saran
    [3] => mohan
)

After:
Array
(
    [0] => mohan
    [1] => saran
    [2] => aakash
    [3] => ram
)
Let’s see what happens when we pass the key_preserve parameter as TRUE. This preserve the keys.<?php  // PHP function to illustrate the use of array_reverse()function Reverse($array){    return(array_reverse($array, true));}  $array = array("ram", "aakash", "saran", "mohan");  echo "Before:\n";print_r($array);  echo "\nAfter:\n";print_r(Reverse($array));  ?>Output:Before:
Array
(
    [0] => ram
    [1] => aakash
    [2] => saran
    [3] => mohan
)

After:
Array
(
    [3] => mohan
    [2] => saran
    [1] => aakash
    [0] => ram
)

<?php  // PHP function to illustrate the use of array_reverse()function Reverse($array){    return(array_reverse($array, true));}  $array = array("ram", "aakash", "saran", "mohan");  echo "Before:\n";print_r($array);  echo "\nAfter:\n";print_r(Reverse($array));  ?>
Output:
Before:
Array
(
    [0] => ram
    [1] => aakash
    [2] => saran
    [3] => mohan
)

After:
Array
(
    [3] => mohan
    [2] => saran
    [1] => aakash
    [0] => ram
)

Reference:http://php.net/manual/en/function.array-reverse.php
PHP-array
PHP-function
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
Download file from URL using PHP
Split a comma delimited string into an array in PHP
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?