如何通过每一个对象的属性来排序对象的数组 [英] How to sort an array of objects by a property of each object

查看:104
本文介绍了如何通过每一个对象的属性来排序对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从存储在一个变量的eventbrite.com API返回的数据数组名为$ restrictedEvents它看起来像下面的数据。这是粘贴在这里的目的只有一个事件的再次presentative但它有一个约80 stdClass的对象像这样完整的数组

我想这个数组在每个stdClass的对象的[标题]键按字母顺序排序。我已经尝试使用:

  usort($ restrictedEvents,称号);

然而,这将返回以下错误:

 警告:usort()[function.usort]:在model.php无效的比较功能上线109

我的猜测是它无法找到标题关键,因为这是一个新的水平下降。在我要去的地方错了,我怎么可以按标题将大大AP preciated任何指针。非常感谢。

 阵列

[4791063199] => stdClass的对象
    (
        [box_header_text_color] => 393837
        [link_color] => EE6600
        [box_background_color] => FFFFFF
        [box_border_color] => D9D4D0
        [时区] =>欧洲/伦敦
        [主办] => stdClass的对象
            (
                [URL] => http://www.eventbrite.com/org/2866607767
                [说明] =>
                [LONG_DESCRIPTION] =>
                [ID] => 2866607767
                [名] => B&安培;问Manifestival
            )        [BACKGROUND_COLOR] => E3DFDC
        [ID] => 4791063199
        【类别】= GT;
        [box_header_background_color] => F0ECE9
        [容量] => 20
        [num_attendee_rows] => 0
        [标题] =>封闭测试活动
        [起始日期] => 2012-11-07 19:00:00
        [现状] =>生活
        [说明] => Lorem存有
        [END_DATE] => 2012-11-07 21:00:00
        [标签] =>
        [了timezone_offset] => GMT + 0000
        [text_color] => 393837
        [title_text_color] =>
        [密码] =>
        [机票] =>排列
            (
                [0] => stdClass的对象
                    (
                        [票] => stdClass的对象
                            (
                                [说明] =>
                                [END_DATE] => 2012-11-07 17:00:00
                                [分钟] => 1
                                [MAX] => 1
                                [价格] => 0.00
                                [quantity_sold] => 0
                                [可见] =>真正
                                [外汇] =>英镑
                                [quantity_available] => 20
                                [类型] = GT; 0
                                [ID] => 15940001
                                [名] => Manifestival事件
                            )                    )            )        [创建] => 2012-11-07十时40分36秒
        [URL] => http://www.eventbrite.com/event/4791063199
        [box_text_color] => 393837
        [隐私] =>私人的
        [地点] => stdClass的对象
            (
                [城市] =>
                [名] => HR培训室
                [国家] =>
                [区域] =>
                [经度] => 0
                [postal_ code =>
                [address_2] =>
                [地址] =>
                [纬度] => 0
                [country_ code =>
                [ID] => 2619469
                [纬度龙] => 0.0 / 0.0
            )        [修改] => 2012-11-07 10点47分20秒
        [重复] =>没有
    )


解决方案

第二paramater到 usort 应该是一个功能。请参见 http://php.net/manual/en/function.usort.php 。你需要传递一个功能,如:

 函数CMP($ A,$ B)
{
    返回STRCMP($ A->标题,$ B->标题);
}

我觉得您需要调用它像 usort($ restrictedEvents,CMP);

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
    )

解决方案

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");.

这篇关于如何通过每一个对象的属性来排序对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆