Hello! There is an array that is sorted by value - from the largest to the smallest. How to sort it also by keys so that the keys with the same values go in alphabetical order? Thank!
1 answer
You can use the array_multisort function to do this .
Example:
<?php $array = [ 'tangerine' => 3, 'plum' => 1, 'apple' => 1 ]; array_multisort(array_values($array), SORT_DESC, array_keys($array), SORT_ASC, $array); Result:
array(3) { ["tangerine"]=> int(3) ["apple"]=> int(1) ["plum"]=> int(1) } First, we sort by the value of the array, then sort by keys and add $ array as the last parameter to sort by the common key.
- Super! Thank you so much) - humster_spb
|
