1

I have an associative array, but I want to sort order them by the 'cat_name

$test = Array
(
    [0] => Array
        (
            [cat_id] => 5
            [cat_name] => Resolution
        )

    [1] => Array
        (
            [cat_id] => 6
            [cat_name] => Speed
        )

    [2] => Array
        (
            [cat_id] => 7
            [cat_name] => Accuracy 
        )
)

When I do sort($test), it sorts the array in order of 'cat_id', how do I sort it by 'cat_name'?

Source
  • 1,026
  • 4
  • 11
  • 23
  • 1
    One way would be to add the 'cat_name' into the array before the 'cat_id', then it will just sort by the first associated element in the array. – Jack Queen Sep 19 '16 at 23:02
  • We need to add to or edit this dupe or find a better one. No `array_column()` in any of the answers. – AbraCadaver Sep 19 '16 at 23:09
  • @AbraCadaver why? no need to edit, deceze's answer (the first one) should suffice, it has the `usort` + `strcmp` flavor. it should sort that by `cat_name` – Kevin Sep 19 '16 at 23:19
  • if this came from the db, why not do the sorting there instead – Kevin Sep 19 '16 at 23:20

0 Answers0