按值对Array中的对象数组进行排序

时间:2012-10-05 20:36:26

标签: php sorting object

基本上我正在尝试对数组中复杂的对象数组进行排序:

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
                )

        )

)

我需要按events_date排序,并确保顶层Object(eg190515)中的数据不会被其他对象破坏,并包含“times”[array]可能存在第二个occurrence _date的可能性对象。

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

基本上我尝试过的。

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

提前致谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

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

  • 您需要uasort功能,以保留关联键
  • 您无法直接从occurrence_date$x访问$y密钥,您需要$x->times[0]->occurence_date
  • 在进行比较之前,请仔细检查$x->times以检查是否有更多条目,选择一个符合您需求的条目。
  • 由于occurence_date是一个数字,因此您不必使用字符串比较功能
祝你好运:)