PHP Array Reverse Function With Examples

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.

PHP array_reverse() function

Definition:- The PHP array_reverse() function is in-built PHP function, which is used to reverse the items or elements of the array.

Syntax:

The syntax of array_reverse() function is following.

array_reverse(array, preserve)

Parameters Of array reverse function:

The function accepts two parameters first one is an array and the second one is preserve (TRUE/FALSE)

  • Array:- The array parameter is required, and it describes the array.
  • preserve:- This is the second parameter of this function and It is an optional parameter. It specifies if the function should preserve the keys of the array or not. Possible values: true, false.

Let’s take examples of the array_reverse() function are

1.One Dimensional Array Reverse In PHP

Here, we will take an example with PHP array_reverse() function. We have one a numeric array, it contains numeric values in it. We will reverse a array using the array reverse function:

See the given below example. This example will reverse a given array:

<?php
   
  $numericArray = array(5, 11, 17, 40, 59);
  $resNumericArray = array_reverse($numericArray);
  print_r($resNumericArray);
 
?>

The output of the above code is

Array
(
    [0] => 59
    [1] => 40
    [2] => 17
    [3] => 11
    [4] => 5
)

2. Associative Array Reverse In PHP

Here, we will take an example with associative array in PHP. We have one an associative array, it contains values in key value pair in it. We will reverse an array using the array reverse function:

See the given below example. This example will reverse a given array:

<?php
   
 
  $keyValueArray = array("a"=>"PHP","b"=>"JAVA","c"=>".NET");
  $resKeyValueArray = array_reverse($keyValueArray);
  print_r($resKeyValueArray);
 
?>

The output of the above code is

Array 
( 
    [c] => .NET 
    [b] => JAVA 
    [a] => PHP 
)

3. PHP reverse multidimensional array

Let’s take a new example with the multidimensional array. We have one multidimensional array and we will reverse this array values or items using the array_reverse() function.

See the given below example.

<?php
   
$array = array(
 
         array("a"=>"PHP","b"=>"JAVA","c"=>".NET"),
         array("d"=>"javascript","e"=>"c","f"=>"c#"),
);
  $reverseArray = array_reverse($array);
  print_r($reverseArray);
 
?>

The output of the above code is:

Array 
( 
 [0] => Array ( [d] => javascript [e] => c [f] => c# ) 
 [1] => Array ( [a] => PHP [b] => JAVA [c] => .NET ) 
)

How to reverse an array in PHP without function?

You can use the for loop the and unset() function of PHP to reverse an array without using the function in PHP.

<?php
$arr = ['a','b','c','d','e','f'];
 
for($i=count($arr)-1;$i>=0;$i--){
$arr[]=$arr[$i];
unset($arr[$i]);
}
print_r($arr);
 
?>

The output of the above code is:

Array 
( 
    [6] => f 
    [7] => e 
    [8] => d 
    [9] => c 
    [10] => b 
    [11] => a 
)

Leave a Reply

Your email address will not be published.