-1

We have an array and need to sort the above array

Array
(
    [0] => stdClass Object
        (
            [id] => 229
            [firstname] => ggg
            [lastname] => fff
        )

    [1] => stdClass Object
        (
            [id] => 230
            [firstname] => aaa
            [lastname] => jjj
        )

)

I want to sort the array as (Sort by firstname)

Array
(
    [0] => stdClass Object
        (
            [id] => 230 
            [firstname] => aaa
            [lastname] => jjj

        )

    [1] => stdClass Object
        (
            [id] => 229
            [name] => ggg
            [lastname] => fff
        )

)
diEcho
  • 52,196
  • 40
  • 166
  • 239
Sateesh
  • 11
  • 1
  • 2

1 Answers1

3

Use usort:

usort($ar, function($a, $b) {
  return strcmp($a->firstname, $b->firstname);
});
phihag
  • 263,143
  • 67
  • 432
  • 458
  • 1
    +1, and in case of PHP < 5.3 use `usort($ar, create_function('$a,$b', 'return strcmp($a->firstname, $b->firstname);'));`. Or declare a function with a name use it. – Paul Aug 30 '11 at 07:17