0

I have an array like this

Array
(
    [73] => Array
        (
            [id] => 73
            [firstName] => Laura
            [lastName] => ...
            [email] => ...
            [password] => 6d1d3a1dcb9e44eb43605f8ad3c529dd7271749c
            [venueId] => 8
            [departmentId] => 2
            [active] => 1
        )

    [116] => Array
        (
            [id] => 116
            [firstName] => Rachael
            [lastName] => ...
            [email] => ...
            [password] => 33d83a16aa038e775709fc8d499fe608ad2f4afe
            [venueId] => 24
            [departmentId] => 1
            [active] => 1
        )

...etc

I want to sort it so the firstName's are in alphabetical order. Is there a prebuilt PHP function I could use, or would I have to sort it manually?

TMH
  • 6,096
  • 7
  • 51
  • 88

1 Answers1

5

Use usort()

function cmp($a, $b) {
    if ($a['firstName'] == $b['firstName']) {
        return 0;
    }
    return ($a['firstName'] < $b['firstName']) ? -1 : 1;
}

usort($array, "cmp");
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • This is an exact duplicate and already has an answer [here](http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value). – Amal Murali Jan 15 '14 at 15:04
  • I saw your close vote and have added my close vote as well – John Conde Jan 15 '14 at 15:05
  • I see it now. May I ask why you VTC'd the question after answering it? Why do we need to close questions? – Amal Murali Jan 15 '14 at 15:14
  • Sometimes when someone sees a question they can answer their first thought is to answer it. In my case, after doing so I saw the close vote and agreed that it was a dupe. It's natural after taking the time to write out an answer to not want to undo your work so I didn't delete it. But with the question being closed as a dupe, anyone searching for something similar in the future will find the canonical question and not this one. – John Conde Jan 15 '14 at 15:18