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 String

PHP string is a sequence of characters. It is used to store and manipulate text. We can specify a string literal in four ways: single quoted double quoted heredoc syntax ...

2 minutes read.

PHP ucwords() Function

PHP ucwords() Function The ucwords() function in PHP converts the first character of each word in the specified string to uppercase. It is a binary-safe function. The related function are as follows: ucfirst()lcfirst()strtoupper()strtolower() Syntax ucwords ( string $str [, string $delimiters ] ) Parameter str(required)- This parameter represents...

1 minute read.

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

1 minute read.

PHP strtoupper() Function

The strtoupper() function in PHP is used to convert a string to uppercase. This function is binary-safe. The related functions are as follows: strtolower()lcfirst()ucfirst()ucwords() Syntax strtoupper ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

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 str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

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 strcasecmp() Function

PHP strcasecmp() Function The strcasecmp() function in PHP compares two strings. It is a binary-safe case-insensitive string function. Syntax strcasecmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than( <...

1 minute read.

PHP is_null() Function

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

1 minute read.

PHP array_change_key_case() Function

PHP array_change_key_case() Function The array_change_key_case() function in PHP is used to change the case of all keys present in the specified array. It returns an array with all keys in the specified array to lowercase or...

1 minute 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 convert_uuencode() Function

PHP convert_uuencode() Function The convert_uuencode() function in PHP is used to encode a string and returns the uuencoded data. Syntax convert_uuencode ( string $data ) Parameter data(required)- This parameter represents the data to be encoded. Return This function returns the uuencoded data or FALSE on failure. Example 1 <?php //...

2 minutes 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 Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes 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 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 sha1() Function

PHP sha1() Function The sha1() in PHP calculates the SHA-1 hash of a string. Syntax sha1 ( string $str [, bool $raw_output = FALSE ] )  Parameter str (required)- This parameter represents the input string. raw_output(optional)- This parameter takes Boolean value and returns the digest in raw binary format...

1 minute read.

PHP foreach Loop

PHP foreach Loop with Example PHP foreach loop is used to loop through the associative arrays. Syntax: foreach($array as $key => $value) { //Statement } $array = associative array. $key = array key. &value =...

2 minutes read.

PHP Multidimensional Array

An array of arrays is called multidimensional array. It is used to store the data in a tabular form. It is represented in the form of matrix (row*column). Example: We can store...

2 minutes read.