PHP array_reverse() Function

PHP array_reverse() Function

The array_reverse() function in PHP returns a new array with the elements in the reversed order.

Syntax

array_reverse (array $array [, bool $preserve_keys = FALSE ] )

Parameter

array(required)- This parameter represents the input array

preserve_keys (optional)- This parameter specifies if the function should preserve the keys of the array or not. It takes a Boolean value, and if set to true, the numeric keys are preserved else for Boolean false (default value), the keys are not preserved.

Return

This function returns the reversed array.

Example 1

 

Output

Array : Array
 (
     [0] => A
     [1] => b
     [2] => c
     [3] => d
 )
 Reversed Array:  
 Array
 (
     [0] => d
     [1] => c
     [2] => b
     [3] => A
 ) 

Example 2

 

Output

Initialized Array : Array
 (
     [0] => apple
     [1] => banana
     [2] => guava
     [3] => orange
 ) 
 Reversed Array: 
 Array
 (
     [3] => orange
     [2] => guava
     [1] => banana
     [0] => apple
 ) 

Example 3

 

Output

Initialized Array : Array
 (
     [0] => php
     [1] => Python
     [2] => C#
     [3] => java
     [4] => Array 
         (
             [0] => Spring
             [1] => Hibernate
         )
 )
 Reversed Array:  
 Array
 (
     [4] => Array
         (
             [0] => Spring
             [1] => Hibernate 
         )
     [3] => java
     [2] => C#
     [1] => Python
     [0] => php
 ) 

Example 4

"a",2=>"b",3=>"c");
 echo("Initialized Array : ");
 print_r($array);
 //Return an array with elements in reverse order while setting the preserve_value true
 $reversed_array = array_reverse($array, true);//keys are preserved
 echo("\nReversed Array while preserving the keys: \n"); 
 //printing the reversed array
 print_r($reversed_array);
 //Return an array with elements in reverse order while setting the preserve_value false
 $reversed_array = array_reverse($array, false);//keys are not preserved
 echo("\nReversed Array while not preserving the keys: \n");
 //printing the reversed array 
 print_r($reversed_array);
 ?> 

Output

Initialized Array: Array
 (
     [1] => a
     [2] => b
     [3] => c
 )
 Reversed Array while preserving the keys: 
 Array 
 (
     [3] => c
     [2] => b
     [1] => a
 ) 
 Reversed Array while not preserving the keys: 
 Array
 (
     [0] => c
     [1] => b
     [2] => a
 ) 

Related Topics

PHP ord() Function

PHP ord() Function The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an...

1 minute read.

PHP is_double() Function

The PHP is_double() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP strspn() Function

PHP strspn() Function The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter. Syntax strspn(string$subject,string$mask[,int$start[,int$length]] ) Parameter subject(required)- This parameter represents the input string. mask(required)- This parameter specifies the characters to find. start (optional)- It signifies the position in the string to start...

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.

PHP strcoll() Function

PHP strcoll() Function The strcoll() function in PHP compares two strings. This function is case-sensitive but not binary-safe. It uses the current locale for doing the comparisons. Syntax strcoll ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter...

1 minute read.

PHP get_defined_vars() Function

PHP get_defined_vars() Function The get_defined_vars() function in PHP returns an array of all defined variables. It returns a multidimensional array containing a list of all defined variables. Syntax get_defined_vars ( void ) Parameter NA Return This function returns a multidimensional array containing a list...

1 minute read.

PHP sort() Function

PHP sort() Function The sort() function in PHP sorts an array where the elements will be arranged from lowest to highest. Syntax sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the...

1 minute read.

PHP Array Functions

There are various array functions in PHP. They are used to access and manipulate the elements ofan array. Following are functions of array. Array Function Description array() It is used to create an array. array_change_key_case() It is...

3 minutes read.

Python main() function

What is a main() function? In most of the programming languages, there is this one special function i.e., main() which is executed firstly everytime when we run the program. The special...

6 minutes read.

PHP rsort() Function

PHP rsort() Function The rsort () function in PHP is used to sort an array in reverse order i.e., highest to lowest. Syntax rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP htmlentities() Function

PHP htmlentities() Function The htmlentities() function in PHP converts all characters to HTML entities. Syntax htmlentities(string,flags,character-set,double_encode);   Parameter string(required)- This parameter specifies the string to convert. flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The...

1 minute read.

PHP array_key_first() function

The array_key_first () function in PHP gets the first key of an array if the specified array is not empty. Syntax array_key_first(array;$array) Parameter array(required)- This parameter signifies the input array. Return This function returns the first key of the specified...

1 minute read.

PHP setlocale() Function

PHP setlocale() Function The setlocale() function in PHP sets locale information(language, monetary, time and, other information specific for a geographical area.) Syntax setlocale ( int $category , array $locale ) Parameter category(required)- This parameter specified a named constant specifying the category of the functions affected...

1 minute read.

PHP end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

1 minute read.

PHP htmlspecialchars() Function

PHP htmlspecialchars() Function The htmlspecialchars() function in PHP converts some special predefined characters to HTML entities. The special predefined characters are as follows: & (ampersand) OR &amp;" (double quote) OR &quot;' (single quote) OR &#039;<...

2 minutes read.

PHP var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

PHP array_fill() function

PHP array_fill() function The array_fill() function in PHP fills an user-defined array with num entries of the value that of specified values parameter, where the keys start at the start_index parameter. Syntax array_fill ( int $start_index , int $num , mixed $value ) Parameter start_index(required)- This parameter represents the first index of the...

1 minute read.

PHP rtrim() Function

PHP rtrim() Function The rtrim() function in PHP is used to strip off the whitespace (or other characters) from the end of a string. If the second parameter is not specified, this function will strip are...

1 minute read.

PHP array_udiff_assoc() Function

PHP array_udiff_assoc() Function The array_udiff_assoc () function in PHP computes the difference between the specified arrays with additional index check and compares the data by a callback function. It returns all the values from array1 that are not...

1 minute read.