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 )
