Linux Hint Posted April 25, 2023 Share Posted April 25, 2023 While working with arrays in PHP, we may need to reverse the order of an array. For this, we have a predefined PHP function called array_reverse(). This function allows us to reverse the order of any input array. This article covers the array_reverse() function in detail, including its syntax, parameters, and example codes. What is the array_reverse() Function in PHP? The array_reverse() function can reverse or reorder the array elements. This function takes an array as its input and returns a new array with the order of the elements reversed. Syntax The syntax for the array_reverse() function is as follows: array_reverse(array, preserve_keys) The function takes an array as its first argument and an optional Boolean value as its second argument. If the second parameter preserve_keys is set to TRUE, the function will preserve the keys of the array. If the second parameter is not set or set to FALSE, the function will reset the keys of the array. By default, a FALSE value is assigned to this parameter. Parameter The array_reverse() function takes two parameters: Array: The array to be reversed. This is a required parameter. preserve_keys: An optional Boolean value that determines whether to preserve the keys of the array or not. It is set to FALSE by default. Return A reversed array is returned by array_reverse() function. Example 1: Reversing an Array In the code given below, we will create an array of numbers and then use the array_reverse() function to reverse the order of the elements in the array without preserving the keys: <?php $numbers = array(1, 2, 3, 4, 5); $reversed_numbers = array_reverse($numbers); print_r($reversed_numbers); ?> Output In the output, we can see the array_reverse() function has reversed the order of the elements in the array without preserving the keys: Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 ) Example 2: Reversing an Array and Preserving Keys Now we will create an array of strings and then using the array_reverse() function we will reverse the order of the elements in the array while preserving the keys: <?php $numbers = array(1, 2, 3, 4, 5); $reversed_numbers = array_reverse($numbers, True); print_r($reversed_numbers); ?> Output Here, the array_reverse() function has reversed the order of the elements in the array while preserving the keys: Array ( [4] => 5 [3] => 4 [2] => 3 [1] => 2 [0] => 1 ) Example 3: Reversing a Multidimensional Array In the code below we have created a multidimensional array and then using the array_reverse() we will reverse the order of array elements: <?php $animals = array( array('name' => 'dog', 'color' => 'brown'), array('name' => 'cat', 'color' => 'gray'), array('name' => 'bird', 'color' => 'blue') ); $reversed_animals = array_reverse($animals); print_r($reversed_animals); ?> Output In the output, we can see the array_reverse() function has reversed the order of elements present inside a multidimensional array: Array ( [0] => Array ( [name] => bird [color] => blue ) [1] => Array ( [name] => cat [color] => gray ) [2] => Array ( [name] => dog [color] => brown ) ) Conclusion With array_reverse() in PHP, we can reorder the elements of an array. A new array is returned by this function, with the elements rearranged according to the input array. This article covers the details of array_reverse() function. For a complete description of the syntax, parameters, and return value of this function read the article. View the full article Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.