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

1

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use uasort in combination with strcmp:

uasort($array, function ($a, $b) {
    return strcmp($a['room'], $b['room']);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.