Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a multidimensional array by string? [duplicate]

I need to sort this array by the subsubkey "description" ascending. I tried a few methods like usort, ksort, subval_sort but none of these work (I guess the main problem is that these are strings, always)

Any help is appreciated

array(77) {
  [0]=>
  array(3) {
    ["name"]=>
    string(17) "abcd"
    ["description"]=>
    string(15) "Delete XY"
    ["level"]=>
    int(1)
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(13) "fgfgdgfd"
    ["description"]=>
    string(18) "Uploader XY"
    ["level"]=>
    int(1)
  }
  [2]=>
  array(3) {
    ["name"]=>
    string(15) "sdfdsfsdfs"
    ["description"]=>
    string(20) "Download abc"
    ["level"]=>
    int(0)
  }
}
like image 717
peipst9lker Avatar asked Mar 12 '12 08:03

peipst9lker


1 Answers

usort($array, function ($a, $b) {
    return strcasecmp($a['description'], $b['description']); //compare two strings ignoring case
});
like image 66
deceze Avatar answered Oct 19 '22 14:10

deceze