PHP arsort() function

PHP arsort() function

The arsort() function in PHP sorts an array in the reverse order and maintains the index association

Syntax

arsort ( 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 behavior of the sort. The various possible values for sorting type flags are as follows:

  • SORT_REGULAR – It is used to compare items normally (don't change types)
  • SORT_NUMERIC – This flag is used to compare the items numerically.
  • SORT_STRING – It compares the given items as strings.
  • SORT_LOCALE_STRING – This flag compares the items as strings, based on the current locale.
  • SORT_NATURAL – It is used to compare items as strings using "natural ordering" like natsort().
  • SORT_FLAG_CASE – It can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort the given strings case-insensitively

Return

This function returns a Boolean value TRUE on success or FALSE on failure.

Example 1

"Reema", "Age"=>"23","Country"=>"India");
 //original array
 echo("Original array: \n");
 print_r($array);
 //sorting the array in reverse order and maintaining the index association
 arsort($array);
 //printing the reversed array 
 echo("\nReversed array: \n");
 print_r($array);
 ?> 

Output

Original array: 
 Array
 (
     [Name] => Reema
     [Age] => 23
     [Country] => India
 )
 Reversed array:  
 Array
 (
     [Name] => Reema
     [Country] => India
     [Age] => 23
 ) 

Example 2

 $val) {
     echo "fruits[" . $key . "] = " . $val . "\n";
 }
 //Sorting the array in reverse order and maintaining the index
 arsort($fruits); 
 echo("\n\nAfter sorting the array in reverse order...\n Reversed Array: \n");
 foreach ($fruits as $key => $val) {
     echo "fruits[" . $key . "] = " . $val . "\n";
 }
 ?> 

Output

Original Array: 
 fruits[0] = lemon
 fruits[1] = orange
 fruits[2] = banana
 fruits[3] = apple
 After sorting the array in reverse order...
  Reversed Array:  
 fruits[1] = orange
 fruits[0] = lemon
 fruits[2] = banana
 fruits[3] = apple 

Example 3

 array(1 => "mango", 2 => "banana", 2 => "apple"),
     "numbers" => array(1, 2, 3, 4, 5, 6),
     "values"   => array("first", 5 => "second", "third")
 );
 //sorting the array in reverse order maintaining the actual keys
 arsort($array); 
 //printing the reversed array
 print_r($array);
 ?> 

Output

Array
 (
     [numbers] => Array
         (
             [0] => 1
             [1] => 2
             [2] => 3
             [3] => 4
             [4] => 5
             [5] => 6 
         )
     [fruits] => Array
         (
             [1] => mango
             [2] => banana
             [3] => apple
         ) 
     [values] => Array
         (
             [0] => first
             [5] => second
             [6] => third
         )
 ) 

Example 4

"Reema", "Age"=>23,"Country"=>"India");
 //original array
 echo("Original array: \n");
 print_r($array);
 //sorting the array in reverse order while maintaining the index association
 arsort($array );
 //printing the sorted array
 echo("\nSorted array without the second parameter: \n");
 print_r($array); 
 //sorting array with second parameter as SORT_STRING in reverse order
 arsort($array,SORT_STRING  );
 //printing the sorted array
 echo("\nSorted array with the second parameter: \n");
 print_r($array);
 ?> 

Output

Original array: 
 Array
 (
     [Name] => Reema
     [Age] => 23
     [Country] => India
 ) 
 Sorted array without the second parameter: 
 Array
 (
     [Country] => India
     [Name] => Reema
     [Age] => 23
 )
 Sorted array with the second parameter:  
 Array
 (
     [Age] => 23
     [Country] => India
     [Name] => Reema
 ) 

Related Topics

PHP strstr() Function

PHP strstr() Function The strstr() function in PHP finds the first occurrence of a string. It returns part of the parameter haystack string starting from and including the first occurrence of needle to the end of the haystack parameter. This function is case-sensitive.  Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] )  Parameter  haystack(required)-...

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

The PHP is_countable() function in PHP is used to  find whether a variable is countable or not. It returns a Boolean value true if the parameter var is countable else it returns false. Syntax is_countable ( mixed...

1 minute read.

PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include...

3 minutes read.

PHP pos() Function

PHP pos() Function The pos() function in PHP returns the current element in an array which is initialized to the first element of the array. This function is an alias of current(). Syntax pos ( array...

1 minute read.

PHP serialize() Function

PHP serialize() Function The serialize() function in PHP is used to convert a storable representation of a value. Syntax serialize ( mixed $value ) Parameter value(required)- This parameter represents the value to be serialized. Return This function returns a string containing a byte-stream representation...

1 minute read.

PHP money_format() Function

PHP money_format() Function The money_format() function in PHP formats a number as a currency string and returns a formatted version of number. Syntax money_format ( string $format , float $number )  Parameter format(required)- This parameter specifies the string to be formatted and how to format the...

1 minute read.

PHP array_multisort() Function

PHP array_multisort () Function The array_multisort() function in PHP sorts multiple or multi-dimensional arrays at once. Syntax array_multisort ( array &$array1 [, mixed $array1_sort_order [, mixed $array1_sort_flags [, mixed $... ]]] ) Parameter array1(required)- This parameter specified the input array to sort. array1_sort_order(optional)- This parameter specifies the order (ascending or descending)...

1 minute read.

PHP array_uintersect_assoc() Function

PHP array_uintersect_assoc() Function The array_uintersect_uassoc() function in PHP calculates the intersection of arrays by comparing the data and indexes by separate callback functions. It returns an array containing all the values of the first input...

2 minutes read.

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

PHP array_diff_key () Function The array_diff_key () Function in PHP compares the keys from array1 against the keys from arrays. It returns an array containing all the entries from array1 whose keys are absent from all other arrays. This function is like array_diff () except that...

1 minute read.

PHP chr() Function

PHP chr() Function The chr() function in PHP generates a single byte string from a number and returns a one-character string containing the character as specified by the unsigned integer. Syntax chr(bytevalue);    Parameter bytevalue(required)- This...

1 minute read.

PHP isset() function

PHP isset is used to check whether the variable is set or not.The PHP isset() function returns false if variable contains a NULL value. Syntax: isset(variable1, variable2….) Presentation of PHP isset: In...

2 minutes read.

PHP strtok() Function

PHP strtok() Function The strtok() function in PHP splits a string into smaller strings or tokenize the specified string.  Syntax strtok ( string $str , string $token ) Parameter str(required)- This parameter represents the input string to split into smaller strings or tokens. token(required)- It signifies...

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

The array_diff () function in PHP compares array1 against other specified arrays and returns the values in array1 that are not present in any of the other arrays. Syntax array_diff(array$array1,array$array2[,array$...] ) Parameter array1(required)- This parameter signifies the input array to...

1 minute read.

PHP reset() Function

PHP reset() Function The reset() function in PHP is used to set the internal pointer of an array to its first element and returns the value of the first array element. Syntax reset ( array &$array )  Parameter array(required)- This parameter represents...

1 minute read.

PHP natsort() Function

PHP natsort() Function The natsort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natsort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean value TRUE on success or FALSE on...

1 minute read.