0

I have one array, say xyz
below is output of var_dump($xyz);

array
    7399 => 
        array
            'count' => int 103
            'name' => string '2º SEMESTRE - 2012' (length=23)
    7398 => 
        array
            'count' => int 50
            'name' => string '1º SEMESTRE - 2012' (length=23)
    7397 => 
        array
            'count' => int 43
            'name' => string '2º SEMESTRE - 2011' (length=23)
    7396 => 
        array
            'count' => int 20
            'name' => string '1º SEMESTRE - 2011' (length=23)
    7395 => 
        array
            'count' => int 53
            'name' => string '2º SEMESTRE - 2010' (length=23)
    'others' => 
        array
            'name' => string 'Others' (length=6)
            'count' => int 65

I want to sort this array by count desc, name asc leaving 'others' element at the bottom. I have used array_multisort like below

// Obtain a list of columns
foreach ($xyz as $key => $row) {
    $count[$key]  = $row['volume'];
    $name[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($count, SORT_DESC, $name, SORT_ASC, $xyz);

but failed. It show following warning:

Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent

Want i want is something like this

array
    7399 => 
        array
            'count' => int 103
            'name' => string '2º SEMESTRE - 2012' (length=23)
    7395 => 
        array
            'count' => int 53
            'name' => string '2º SEMESTRE - 2010' (length=23)
    7398 => 
        array
            'count' => int 50
            'name' => string '1º SEMESTRE - 2012' (length=23)
    7397 => 
        array
            'count' => int 43
            'name' => string '2º SEMESTRE - 2011' (length=23)
    7396 => 
        array
            'count' => int 20
            'name' => string '1º SEMESTRE - 2011' (length=23)
    'others' => 
        array
            'name' => string 'Others' (length=6)
            'count' => int 65

Please help. Thanking you all in anticipation!!!

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
  • Did you try with `usort()` ? – romainberger Jan 19 '13 at 14:25
  • @romainberger: usort sorts the array with user-defined function. If i would be able to do define a function that can sort array/solve my problem, why would i post a question her. Anyways, Thanks for replying – Bhavik Shah Jan 19 '13 at 14:29

1 Answers1

1

There's already been a well-written answer for this problem:

function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

So, to sort by count, you do:

aasort($your_array,"count");
Community
  • 1
  • 1
hohner
  • 11,498
  • 8
  • 49
  • 84
  • i dont know why it doesnt worked when i tried what you have specified in your answer but the link that you have specified in your answer has helped me a lot. Thanks for the link. – Bhavik Shah Jan 19 '13 at 14:40
  • i would have loved to. But, its not your answer that has solved my problem. Its the one whose link you have specified. So, i have upvoted you answer. Thanks a lot for that. Seriously... – Bhavik Shah Jan 19 '13 at 14:49