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

Python Set pop() method

Python Set pop() method The set.pop() method in Python removes an arbitrary element (random item) from the set and returns the element removed. Syntax set.pop()  Parameter NA Return This method returns an arbitrary (random) element from the set. Example 1 #...

1 minute read.

PHP current() function

PHP current() function The current() function in PHP returns the current element in an array, which is initialized to the first element inserted into the array. Syntax current ( array $array )   Parameter array (required)– This parameter represents the input array or countable object. Return This...

1 minute read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

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 Switch Statement with Example

PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax: switch(n) {             case1: statement if n=case1; break;             case2:             statement if n=case2; break;             case3:            ...

3 minutes read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.

PHP vfprintf() Function

PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args )  Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...

3 minutes read.

PHP nl_langinfo() Function

PHP nl_langinfo() Function The nl_langinfo() function in PHP returns specific local information. This function does not work on Windows platforms. Syntax nl_langinfo ( int $item ) Parameter Item(required)- This parameter specifies the  integer value of the element or the constant name of the...

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.

PHP array_search() Function

PHP array_search() Function The array_search() function in PHP searches the array for the specified value. This function returns the first corresponding key if the value is found. Syntax array_search ( mixed $needle , array $haystack [, bool $strict ])  Parameter needle(required)- This parameter represents the value to...

1 minute read.

PHP is_bool() Function

The is_bool() Function in PHP is used to  find whether a variable is a Boolean or not. It returns a Boolean value true if the parameter var is a Boolean else it returns...

1 minute read.

PHP sprintf() Function

PHP sprintf() Function The sprintf() function in PHP is used to write a formatted string to a variable and returns a formatted string. PHP 4 and above versions support the sprintf() function. The related functions are...

8 minutes read.

PHP array_push() Function

PHP array_push() Function The array_push() function in PHP pushes one or more elements onto the end of the array. This function increases the length of the array by the number of variables pushed. Syntax array_push ( array &$array, mixed& value [, mixed $... ]...

1 minute read.

PHP break

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

3 minutes read.

PHP range() Function

PHP range() Function The range () function in PHP is used to create an array containing a range of elements. Syntax range ( mixed $start , mixed $end [, number $step = 1 ] ) Parameter start(required)- This parameter represents the first value of the sequence. end(required)- This parameter represents the...

1 minute read.

PHP Associative Arrays

PHP associate array is used to access the elements with keys by “=>” symbol. There are two ways to create an associative array. 1st way: $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); 2nd way: $age['john'] = "20"; $age['Helena'] =...

1 minute read.

PHP settype() Function

PHP settype() Function The settype() function in PHP is used to set the type of variable var to type. Syntax settype ( mixed &$var , string $type ) Parameter var(required)- This parameter represents the variable being converted. type(required)- This parameter represents the type of variable that is needed.  The possible values of type are...

1 minute read.

PHP is_scalar() Function

PHP is_scalar() Function The is_scalar() function in PHP finds whether a variable is a scalar or not. It returns a Boolean value TRUE if the parameter var is scalar, else it returns FALSE. Scalar variables are those containing an integer, float, string or boolean. This...

1 minute read.

PHP sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP compact() function

PHP compact() function The compact() function in PHP is used to create an array containing variables and their values. It returns the output array whose keys are the variable names, and their corresponding values are...

1 minute read.