PHP | Array Reverse Function With Examples

PHP | Array Reverse Function With Examples

Reverse array in PHP. In this tutorial, we would love to share with you, how to reverse array one-dimensional array, associative array and multidimensional array in PHP.

This tutorial also shows you an easy way to reverse an array in PHP without function and reverse number array in PHP.

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, 12, 18, 50, 49);
  $resNumericArray = array_reverse($numericArray);
  print_r($resNumericArray);

?>

The output of the above code is

Array
(
    [0] => 49
    [1] => 50
    [2] => 18
    [3] => 12
    [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 
( 
	 => .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 this example below:

<?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  => .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 
)

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

Leave a Reply

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