0

基本上我试图在一个数组中对一个复杂的对象数组进行排序:

Array
(
    [190515] => stdClass Object
        (
            [nid] => 15740686
            [venue_nid] => 190515
            [occurrences] => 1
            [this_weeks_occurrences] => 0
            [end_date] => 1350853200
            [end_date_end_time] => 1350853200
            [is_ongoing] => 0
            [title] => Wentz Concert Hall and Fine Arts Center
            [times] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 15740686
                            [venue_nid] => 190515
                            [venue_title] => Wentz Concert Hall and Fine Arts Center
                            [datepart] => 20121021
                            [occurrences] => 1
                            [times] => Sun 4:00pm
                            [end_times] => Sun 4:00pm
                            [next_year] => 0
                            [next_month] => 0
                            [next_week] => 3
                            [occurrence_date] => 1350853200
                        )

                )

            [times_list] => Array
                (
                    [0] => Oct 21 sun 4:00pm
                )

        )

    [31403] => stdClass Object
        (
            [nid] => 15740686
            [venue_nid] => 31403
            [occurrences] => 1
            [this_weeks_occurrences] => 0
            [end_date] => 1350176400
            [end_date_end_time] => 1350176400
            [is_ongoing] => 0
            [title] => KAM Isaiah Israel
            [times] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 15740686
                            [venue_nid] => 31403
                            [venue_title] => KAM Isaiah Israel
                            [datepart] => 20121014
                            [occurrences] => 1
                            [times] => Sat 8:00pm
                            [end_times] => Sat 8:00pm
                            [next_year] => 0
                            [next_month] => 0
                            [next_week] => 2
                            [occurrence_date] => 1350176400
                        )

                )

            [times_list] => Array
                (
                    [0] => Oct 13 sat 8:00pm
                )

        )

    [33861] => stdClass Object
        (
            [nid] => 15740686
            [venue_nid] => 33861
            [occurrences] => 1
            [this_weeks_occurrences] => 0
            [end_date] => 1350781200
            [end_date_end_time] => 1350781200
            [is_ongoing] => 0
            [title] => Music Institute of Chicago, Nichols Concert Hall
            [times] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 15740686
                            [venue_nid] => 33861
                            [venue_title] => Music Institute of Chicago, Nichols Concert Hall
                            [datepart] => 20121021
                            [occurrences] => 1
                            [times] => Sat 8:00pm
                            [end_times] => Sat 8:00pm
                            [next_year] => 0
                            [next_month] => 0
                            [next_week] => 3
                            [occurrence_date] => 1350781200
                        )

                )

            [times_list] => Array
                (
                    [0] => Oct 20 sat 8:00pm
                )

        )

)

我需要按发生日期排序并确保顶部对象(例如190515)中的数据不会被其他对象损坏,并包括对象的“时间”[数组]可能存在第二个发生日期的可能性。

我在这里看到过类似的按字段排序的对象数组,但不是数组/对象的深度。我尝试使用 usort 但我认为我的值语法不正确。

基本上我试过了。

function cmp($x, $y) {
    if ($x->occurrence_date > $y->occurrence_date) {
      return 1; }
    else {
      return -1; }
 }
usort($node->timeout_events_schedule->venues, 'cmp');

提前致谢

4

2 回答 2

3

怎么样:

function compare($x,$y)
{
    if($x->times[0]->occurrence_date == $y->times[0]->occurrence_date)
        return 0;
    elseif($x->times[0]->occurrence_date < $y->times[0]->occurrence_date)
        return -1;
    else
        return 1;

}

uasort($your_array,'compare');

uasort() 保留您的密钥,不像 usort() http://www.php.net/manual/en/function.uasort.php

于 2012-10-05T20:39:01.307 回答
2

帮助您解决问题的一些提示:

  • 您将需要uasort功能,以保留关联键
  • 您不能直接从或访问occurrence_date密钥,您需要$x$y$x->times[0]->occurence_date
  • 在进行比较之前,请遍历$x->times以检查是否有更多条目,选择一个适合您需要的条目。
  • 因为occurence_date是一个数字,您不必使用字符串比较功能

祝你好运 :)

于 2012-10-05T21:02:02.480 回答