php - 如何根据每个对象的属性对对象数组进行排序

标签 php arrays sorting

我有一个从 eventbrite.com api 返回的数据数组,存储在一个名为 $restrictedEvents 的变量中,看起来像下面的数据。出于粘贴此处的目的,这只是一个事件的代表,但它在整个数组中有大约 80 个这样的 stdClass 对象。

我想按每个 stdClass 对象中的 [title] 键按字母顺序对这个数组进行排序。我试过使用:

usort($restrictedEvents, "title");

但是这会返回以下错误:

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

我猜它找不到标题键,因为它在下一层。任何关于我哪里出错以及如何按标题排序的指示将不胜感激。非常感谢。

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
    )

最佳答案

usort 的第二个参数应该是一个函数。参见 http://php.net/manual/en/function.usort.php .您需要向它传递一个函数,例如:

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

我想你会这样调用它 usort($restrictedEvents, "cmp");

关于php - 如何根据每个对象的属性对对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13277365/

相关文章:

java - 如何为在我的网站注册的任何人自动创建电子邮件地址?

php - 如何在php中动态创建嵌套数组

python - 计算 Python 中多维数组中达到或超过阈值的次数

c# - 对格式化时间字符串数组进行排序

Angular Material 排序表 - 如何将标题文本右对齐?

algorithm - 选择算法运行时

php - 在 WooCommerce 中设置我的帐户自定义项目端点标题

php - `require (' file.php ')` 停止我的代码并显示空白页面

php 脚本未在 WAMP 服务器中运行

c - 程序跳过用户输入(C 语言的大型模块化图书馆信息系统)