PHP arsort: Sort Arrays Descending
arsort function is a PHP function used to sort arrays in descending order based on their values. It can sort both associative and indexed arrays while maintaining the key-value associations of the array.
Usage as follows: arsort(array &$array, int $sort_flags = SORT_REGULAR): bool
Explanation of parameters:
- $array: an array that needs to be sorted, passed by reference so that the original array can be modified inside the function.
- $sort_flags: optional parameter that specifies the type of sorting. Default is SORT_REGULAR, which means sorting based on the type of elements.
Return value:
- Return true on success and false on failure.
“我很高兴帮助你们”;
“I am happy to assist you.”
$fruits = array("apple" => 3, "orange" => 2, "banana" => 1);
arsort($fruits);
print_r($fruits);
Output results:
Array
(
[apple] => 3
[orange] => 2
[banana] => 1
)
In the example, the arsort function is used to sort the $fruits array in descending order based on values while maintaining the association between keys and values.