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');
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; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With