6

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)
  }
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
peipst9lker
  • 605
  • 1
  • 5
  • 17

2 Answers2

11
usort($array, function ($a, $b) {
    return strcasecmp($a['description'], $b['description']); //compare two strings ignoring case
});
deceze
  • 510,633
  • 85
  • 743
  • 889
0

you can use SORT_STRING option of array_multisort like:

array_multisort($ar[0], SORT_ASC, SORT_STRING,
          $ar[1], , SORT_ASC, SORT_STRING);
Vikram
  • 8,235
  • 33
  • 47