Thursday, 30 August 2018

PHP How to sort the associative array first by the keys then by the values?

$arr =array(
    28 => 23,
    26 => 23,
    15 => 12,
    29 => 12,
    1 => 12,
    16 => 15,
    30 => 15,
    11 => 12,
    8 => 23,
    33 => 23
);

how to sort like this :
8 => 23
26 => 23
28 => 23
33 => 23
16 => 15
30 => 15
1 => 12
11 => 12
15 => 12
29 => 12


Use uksort, but make the array available to the comparison function for the secondary comparison by value. Making it a global variable would be the quickest + dirtiest way.

0 comments:

Post a Comment