Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an array of objects by a property of each object [duplicate]

I have an array of data returned from the eventbrite.com api stored in a variable called $restrictedEvents which looks like the data below. This is representative of just one event for the purposes of pasting here but it has a about 80 stdClass Objects like this in the full array.

I want to sort this array alphabetically by the [title] key in each stdClass Object. I have tried using:

usort($restrictedEvents, "title");

However this returns the following error:

Warning: usort() [function.usort]: Invalid comparison function in model.php on line 109

My guess is it cannot find the title key as this is in the next level down. Any pointers on where I am going wrong and how I can sort by the title would be greatly appreciated. Many thanks.

Array 
(
[4791063199] => stdClass Object
    (
        [box_header_text_color] => 393837
        [link_color] => EE6600
        [box_background_color] => FFFFFF
        [box_border_color] => D9D4D0
        [timezone] => Europe/London
        [organizer] => stdClass Object
            (
                [url] => http://www.eventbrite.com/org/2866607767
                [description] => 
                [long_description] => 
                [id] => 2866607767
                [name] => B&Q Manifestival
            )

        [background_color] => E3DFDC
        [id] => 4791063199
        [category] => 
        [box_header_background_color] => F0ECE9
        [capacity] => 20
        [num_attendee_rows] => 0
        [title] => Closed Event Test
        [start_date] => 2012-11-07 19:00:00
        [status] => Live
        [description] => Lorem ipsum
        [end_date] => 2012-11-07 21:00:00
        [tags] => 
        [timezone_offset] => GMT+0000
        [text_color] => 393837
        [title_text_color] => 
        [password] => 
        [tickets] => Array
            (
                [0] => stdClass Object
                    (
                        [ticket] => stdClass Object
                            (
                                [description] => 
                                [end_date] => 2012-11-07 17:00:00
                                [min] => 1
                                [max] => 1
                                [price] => 0.00
                                [quantity_sold] => 0
                                [visible] => true
                                [currency] => GBP
                                [quantity_available] => 20
                                [type] => 0
                                [id] => 15940001
                                [name] => Manifestival Event
                            )

                    )

            )

        [created] => 2012-11-07 10:40:36
        [url] => http://www.eventbrite.com/event/4791063199
        [box_text_color] => 393837
        [privacy] => Private
        [venue] => stdClass Object
            (
                [city] => 
                [name] => HR Training Room
                [country] => 
                [region] => 
                [longitude] => 0
                [postal_code] => 
                [address_2] => 
                [address] => 
                [latitude] => 0
                [country_code] => 
                [id] => 2619469
                [Lat-Long] => 0.0 / 0.0
            )

        [modified] => 2012-11-07 10:47:20
        [repeats] => no
    )
like image 524
Ben Paton Avatar asked Jan 28 '26 15:01

Ben Paton


1 Answers

The second paramater to usort should be a function. See http://php.net/manual/en/function.usort.php. You would need to pass it a function like:

function cmp($a, $b)
{
    return strcmp($a->title, $b->title);
}

I think you would then call it like usort($restrictedEvents, "cmp");.

like image 139
Joshua Dwire Avatar answered Jan 30 '26 04:01

Joshua Dwire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!