如何通过每个对象的属性对对象数组进行排序

时间:2012-11-07 20:08:38

标签: 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
    )

4 个答案:

答案 0 :(得分:10)

usort的第二个参数应该是一个函数。见http://php.net/manual/en/function.usort.php。你需要传递一个类似的函数:

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

我想你会把它称为usort($restrictedEvents, "cmp");

答案 1 :(得分:3)

您可以使用ouzo goodies

执行此操作
$result = Arrays::sort($restrictedEvents, Comparator::compareBy('title'));

http://ouzo.readthedocs.org/en/latest/utils/comparators.html

答案 2 :(得分:1)

usort的第二个参数是函数,而不是键名。该函数传递给被排序的数组的两个元素,并返回一个值,指示这两个元素应如何相互排序:-1如果它们传递给函数的顺序是正确的,1如果它被反转,如果没关系则为0(两个元素在比较时相等)。以下是您的案例示例:

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

如果您使用的是较旧的PHP(5.3.0之前,它引入了匿名函数),那么您必须为比较函数指定一个名称并将该名称作为字符串传递给usort

function titlecmp($a, $b) {
  return strcmp($a->title, $b->title); 
}
usort($restrictedEvents, "titlecmp");

答案 3 :(得分:-1)

通常这样的事情是通过带有$keyToSort => $id的第二个数组实现的,通过标准排序函数或您自己的数组对此数组进行排序,然后转换到第一个数组

使用它,阵列的深度是无限的。