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

Php 按值对数组中的对象数组进行排序,php,sorting,object,Php,Sorting,Object,基本上,我尝试在数组中对复杂的对象数组进行排序: Array ( [190515] => stdClass Object ( [nid] => 15740686 [venue_nid] => 190515 [occurrences] => 1 [this_weeks_occurrences] => 0 [end_date]

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

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');
提前感谢

怎么样:

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');
函数比较($x,$y)
{
如果($x->times[0]->发生日期==$y->times[0]->发生日期)
返回0;
elseif($x->times[0]->发生日期<$y->times[0]->发生日期)
返回-1;
其他的
返回1;
}
uasort($your_数组,'compare');
uasort()保留密钥,与usort()不同

那么:

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');
函数比较($x,$y)
{
如果($x->times[0]->发生日期==$y->times[0]->发生日期)
返回0;
elseif($x->times[0]->发生日期<$y->times[0]->发生日期)
返回-1;
其他的
返回1;
}
uasort($your_数组,'compare');
uasort()保留密钥,与usort()不同

帮助您解决问题的几个技巧:

  • 您将需要函数来保留关联键
  • 您不能直接从
    $x
    $y
    访问
    发生日期
    键,您需要
    $x->times[0]->发生日期
  • 在进行比较之前,反复浏览
    $x->times
    以检查是否有更多条目,选择一个适合您需要的条目
  • 由于
    occurrence\u date
    是一个数字,您不必使用字符串比较函数

祝你好运:)

一些帮助你解决问题的小贴士:

  • 您将需要函数来保留关联键
  • 您不能直接从
    $x
    $y
    访问
    发生日期
    键,您需要
    $x->times[0]->发生日期
  • 在进行比较之前,反复浏览
    $x->times
    以检查是否有更多条目,选择一个适合您需要的条目
  • 由于
    occurrence\u date
    是一个数字,您不必使用字符串比较函数

祝你好运:)

为什么要将数字与strcmp进行比较?最初我是按时间列表排序的,但改为发生日期。我会更新。你为什么要将数字与strcmp进行比较?最初我是按时间列表排序的,但改为发生日期。我会更新。在取消删除并在25分钟后完全更改答案后,人们应该认为它是正确的,但呈现的代码不会与OP提供的示例数据一起工作。这就是为什么最好指导他人编写自己的代码,让他理解它的工作原理,我的坏朋友D:下次我试了这个,但它没有排序,我会这样做的。还尝试了@dev null居住者的建议,使用变量为:$x->times[0]->occurrence\u date,这导致php死机。很抱歉,我弄乱了compare函数的返回。我编辑了它。谢谢,但即使编辑,它也不能正确排序。可能是我们没有“接触”到变量吗?在['times']之前是否需要[0]或其他内容?在25分钟后取消删除并完全更改答案后,人们应该认为它是正确的,但呈现的代码不会与OP提供的示例数据一起工作。这就是为什么最好指导他人编写自己的代码,让他了解它的工作原理,我的坏朋友D:下次我试了这个,但它没有排序,我会这样做的。还尝试了@dev null居住者的建议,使用变量为:$x->times[0]->occurrence\u date,这导致php死机。很抱歉,我弄乱了compare函数的返回。我编辑了它。谢谢,但即使编辑,它也不能正确排序。可能是我们没有“接触”到变量吗?['times'之前是否需要有[0]或其他内容?