I have a PHP array like the following:
$array = array(
'06930' => array(
'count' => 20
),
'06905' => array(
'count' => 25
),
'06910' => array(
'count' => 15
),
'06903' => array(
'count' => 15
),
'06920' => array(
'count' => 10
),
'06940' => array(
'count' => 5
),
'06915' => array(
'count' => 10
),
);
Contains zip codes and some data (count, ...) related to that zip. I need to sort it based on count (DESC), and then sort it again based on zip code (ASC), so that the result will be as follows:
$array = array(
'06905' => array(
'count' => 25
),
'06930' => array(
'count' => 20
),
'06903' => array(
'count' => 15
),
'06910' => array(
'count' => 15
),
'06915' => array(
'count' => 10
),
'06920' => array(
'count' => 10
),
'06940' => array(
'count' => 5
),
);
Zip with bigger count will be above others, but if two zips have equal count, smaller zip will be above.
I can sort it based on count using:
uasort($array, function ($a, $b) {
return $b['count'] <=> $a['count'];
});
But can't sort based on zip afterwards.
I reviewed many similar questions and answers but still couldn't figure out how to sort second time by the main array key (zip).
Thanks for any tips.