dongtuo1482 2018-06-27 12:50
浏览 72

按日期排序多维数组,但只有它的子项

i'm trying to sort the children of an array but failing miserably.. Problem is that only the children should get sorted/compared against another by their ['date']['form_date '] value, not the first layer of the array :(

  Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-07-07
                            [to_date] => 2018-07-07
                        )

                    [post] => Special: Pferdefotografie
                )

            [1] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-07-21
                            [to_date] => 2018-07-21
                        )

                    [post] => Fotoexkursion Südafrika
                )

            [2] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-07-21
                            [to_date] => 2018-07-21
                        )

                    [post] => Fotoexkursion Landschaftsfotografie
                )

            [3] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-07-21
                            [to_date] => 2018-07-21
                        )

                    [post] => Bildaufbau und Komposition
                )

            [4] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-07-22
                            [to_date] => 2018-07-22
                        )

                    [post] => Bildaufbau und Komposition
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-08-11
                            [to_date] => 2018-08-11
                        )

                    [post] => Bildaufbau und Komposition
                )

            [1] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-08-04
                            [to_date] => 2018-08-04
                        )

                    [post] => Makrofotografie 1
                )

            [2] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-08-26
                            [to_date] => 2018-08-26
                        )

                    [post] => Tierfotografie 2
                )

            [3] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-08-19
                            [to_date] => 2018-08-19
                        )

                    [post] => Tierfotografie 1
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-09-15
                            [to_date] => 2018-09-15
                        )

                    [post] => Bildaufbau und Komposition
                )

            [1] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-09-02
                            [to_date] => 2018-09-02
                        )

                    [post] => Makrofotografie 2
                )

            [2] => Array
                (
                    [date] => Array
                        (
                            [from_date] => 2018-09-01
                            [to_date] => 2018-09-01
                        )

                    [post] => Makrofotografie 1
                )

        )

)

I tried on using usort for comparing the dates, but i guess it fails because of the level of nesting!?

usort($events, function ($a, $b) {
    return strtotime($a[0]['date']['from_date']) - strtotime($b[0]['date']['from_date']);
});

Any help is much appreciated! Thanks in advance for looking into this :)

  • 写回答

1条回答 默认 最新

  • dtotwai720621 2018-06-27 13:59
    关注

    Because your dates are in Y-m-d format, you don't need to bother converting them to timestamps -- just compare them as strings. As deceze said, you just need to iterate the first level and remember to modify by reference (so that you aren't modifying a "copy" of the array).

    Code: (Demo: https://3v4l.org/AXsqR )

    foreach ($array as &$set) {
        usort($set, function ($a, $b) {
            return strcmp($a['date']['from_date'], $b['date']['from_date']);
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”