dsaf415212 2017-09-17 16:15
浏览 51
已采纳

php使用数字键排序多维数组

I'm making an event calendar. Everything needs to be sorted chronologically.

This is the array I'm currently sorting:

$years = array(
    2018 => array(
        01 => array(),
        02 => array()
    ),
    2017 => array(,
        02 => array()
        01 => array()
    )
);

I originally have a larger array used to populate this $years.

Using ksort, I've sorted my years correctly, i.e. they are in chronological order. However, I can't sort my third level (see in 2017, my array is 02,01 whereas it should read 01,02). The last level or the array (such as 2017 > 02) is sorted correctly, this is not an issue, as these are sorted by content within each of them.

I've tried array_multisort, ksort, usort and simply sort but none allow me to sort my keys as they are numeric.

  • 写回答

2条回答 默认 最新

  • douzhuican0041 2017-09-17 16:21
    关注

    How about iterating through the children elements, sorting them with ksort() as well:

    <?php
    $years = array(
        2018 => array(
            01 => array(),
            02 => array()
        ),
        2017 => array(
            02 => array(),
            01 => array()
        )
    );
    ksort($years);
    foreach ($years as &$year) {
        ksort($year);
    }
    var_dump($years);
    

    Demo

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

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入