7

I've a rather large array that looks like

Array(
   [0] => stdClass Object
        (
            [id] => 8585320
            [title] => the title
            [type] => page
            [url] => link.com
            [excerpt] => brief description
        )

    [1] => stdClass Object
        (
            [id] => 8585320
            [title] => the title
            [type] => page
            [url] => link.com
            [excerpt] => brief description
        )
 )

I have no apparent control over the way the array is formed, and how it comes out, it seems as there is little if any logic to it. But I am stuck with it. So what I need to do is basically take the array sort it numerically by each stdClass Object and then make sure the id's are from largest to smallest and not smallest to largest. All the while maintaining the current structure of the array object combination

I can't even begin to think right now how I would need to approach sorting it the way I need it. As its already been a long enough day.

UPDATE

public function updateRet($a, $b) {
        return $b->id - $a->id;
    }
usort($ret, 'updateRet');  
chris
  • 36,115
  • 52
  • 143
  • 252

1 Answers1

13

Just use usort:

function compare_some_objects($a, $b) { // Make sure to give this a more meaningful name!
    return $b->id - $a->id;
}

// ...

usort($array, 'compare_some_objects');

If you have PHP 5.3 or higher, you can also use an anonymous function:

usort($array, function($a, $b) { return $b->id - $a->id; });
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Im getting an invalid comparison function, Im thinking its cause I have an array with the objects inside of it. so I have to get it to do `$b[x]->id` somehow but like that its not working – chris Jun 21 '12 at 00:31
  • @chris: Use the first method then. Make sure the function is defined properly. – Ry- Jun 21 '12 at 00:33
  • thats the problem, not sure how to define it properly I gave it a different name for the function, but essentially left the rest alone, and am using it just as above. But not getting lucky. Could it be also because Im running this in an OOP style coding? can I pass it like $this->compare_some_objects (again changed that name but just for reference here) – chris Jun 21 '12 at 00:36
  • @chris: [It works correctly for me.](http://codepad.org/m59Vrazp) Can you post your code? – Ry- Jun 21 '12 at 00:36
  • updated to show the code – chris Jun 21 '12 at 00:39
  • Yes but your example is 'array(array())' I have 'array(stdClass())' and this is where I think Im running into the issue some where – chris Jun 21 '12 at 00:40
  • @chris: I used `array(array())` because PHP doesn't have object literals. See the `foreach` loop directly afterwards? It converts the arrays to objects. And trust me - there is no difference between the two when it comes to simple property access. OOP is not magically different from regular PHP. – Ry- Jun 21 '12 at 00:41
  • 2
    @chris: Anyway, the problem is that it's a *`public function`*, which means you put it inside a class. It should not be inside a class. If you want it to be inside a class, make it a `public static function` and change `'updateRet'` to `array('YourClassName', 'updateRet')`. – Ry- Jun 21 '12 at 00:43
  • that last bit was my issue. I appreciate the help, thanks for putting up with me as well :-D – chris Jun 21 '12 at 00:53
  • @chris: No problem! I'm glad you got it solved :) – Ry- Jun 21 '12 at 00:54
  • For anyone who needs to sort the IDs ascending rather than descending just change the function to `return $a->id - $b->id;` – Mike Sep 07 '16 at 13:26
  • @Ryan thank you brow!! nice work – Fábio Zangirolami Oct 05 '17 at 15:09