dozrhldy285751 2012-10-05 20:36
浏览 33
已采纳

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

Basically I'm trying to sort a complex array of Objects within an array:

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
                )

        )

)

I need to sort by occurrence_date and ensure that the data in the top Object (e.g.190515) doesn't become corrupted with other objects and to include the possibility that there could be a 2nd occurrence_date for the "times" [array] of Objects.

I've seen similar sort arrays of objects by field here, but not with the depth of the array/object. I tried using usort but I don't think my syntax to the value is correct.

Basically what I tried.

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

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongquepao8653 2012-10-05 20:39
    关注

    How about:

    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() preserve your keys, unlike usort() http://www.php.net/manual/en/function.uasort.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器