0

I have the following array:

Array ( 
    [159] => Array ( [0] => 46 ) 
    [192] => Array ( [0] => 32.2 
                     [1] => 12.3 ) 
    [80] => Array ( [0] => 13.8 ) 
    [328] => Array ( [1] => 41 ) 
    [32] => Array ( [1] => 28.7 ) 
)

I would like to sort the first level array by the sums of the second level. Code so far is:

function compare_array_sum_desc($a, $b) {
    if (array_sum($a) == array_sum($b)) {
        return 0;
    }
    return (array_sum($a) < array_sum($b)) ? 1 : -1;
}

usort($results, 'compare_array_sum_desc');

This sorts the array, but reindexes the keys, which are the pid value from a database, and needs to be retained after the sort.

Or should I add the pid as a value and not a key? If so, can pointers be provided?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
jingo_man
  • 509
  • 3
  • 13

0 Answers0