0

I have an array that outputs data that looks like this:

 [STU-CZC0226PVC] => Array
    (
        [ComputerName] => STU-CZC0226PVC
        [time_in_days] => 13
        [room] => 1N10
    )

[STU-FMDXZDHJF-M] => Array
    (
        [ComputerName] => STU-FMDXZDHJF-M
        [time_in_days] => 13
        [room] => 2R022
    )

[STU-CZC03184CM] => Array
    (
        [ComputerName] => STU-CZC03184CM
        [time_in_days] => 13
        [room] => 2Q11
    )

[STU-CZC0226PTM] => Array
    (
        [ComputerName] => STU-CZC0226PTM
        [time_in_days] => 13
        [room] => 1N10
    )

[STU-CZC12632SN] => Array
    (
        [ComputerName] => STU-CZC12632SN
        [time_in_days] => 13
        [room] => 1N75

I would like to sort the array so that the records with the same room number are listed together, but don't have a clue how to do it. I think it might use usort(), but I can't work out how to implement that, and was wondering if anyone could help?

Thanks

GiANTOnFire
  • 193
  • 1
  • 4
  • 16
  • 1
    Possible duplicate of [Sort Array of MultiDiminsional Arrays on More Than One “Column” (Key) With Specified Sort Options](http://stackoverflow.com/questions/809771/sort-array-of-multidiminsional-arrays-on-more-than-one-column-key-with-speci?rq=1) – Gildas Ross Mar 26 '14 at 14:54

2 Answers2

0

PHP >= 5.5.0

array_multisort(array_column($array, 'room', 'ComputerName'), SORT_DESC, $array);

PHP < 5.5.0

foreach($array as $key => $val) {
    $rooms[$key] = $val['room'];
}
array_multisort($rooms, SORT_DESC, $array);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

You can use uasort in combination with strcmp:

uasort($array, function ($a, $b) {
    return strcmp($a['room'], $b['room']);
});
TimWolla
  • 31,849
  • 8
  • 63
  • 96