duankua3620 2018-03-26 15:22
浏览 42
已采纳

在不同的键上排序多维数组

I have a load of folders and I use DirectoryIterator to get them into a multidimensional array. The outcome of this is something like this

array:10 [▼
  "SomeTitle" => array:2 [▼
    2018 => array:3 [▼
      "February" => array:4 [▶]
      "January" => array:1 [▶]
      "March" => array:1 [▶]
    ]
    2017 => array:11 [▼
      "February" => array:9 [▶]
      "January" => array:12 [▶]
      "March" => array:9 [▶]
      "September" => array:9 [▶]
      "June" => array:8 [▶]
      "December" => array:12 [▶]
      "October" => array:8 [▶]
      "July" => array:10 [▶]
      "April" => array:8 [▶]
      "August" => array:10 [▶]
      "May" => array:10 [▶]
    ]
  ]
]

So I have the main key, followed by year, then month, and then some other data.

What I am trying to do is organise the data by year and month. So 2018 should always be first. I then need the months to be organised in month order. At the moment, I am passing the array to this function

function sortArray($arr) {
    ksort($arr);

    foreach ($arr as $k => $v) {
        if (is_array($v)) {
            $arr[$k] = $this->sortArray($v);
        }
    }
    return $arr;
}

I dont think I need the ksort as this seems to put 2017 first. When I run the above, I get the following

array:10 [▼
  "SomeTitle" => array:2 [▼
    2017 => array:11 [▼
      "April" => array:8 [▶]
      "August" => array:10 [▶]
      "December" => array:12 [▶]
      "February" => array:9 [▶]
      "January" => array:12 [▶]
      "July" => array:10 [▶]
      "June" => array:8 [▶]
      "March" => array:9 [▶]
      "May" => array:10 [▶]
      "October" => array:8 [▶]
      "September" => array:9 [▶]
    ]
    2018 => array:3 [▼
      "February" => array:4 [▶]
      "January" => array:1 [▶]
      "March" => array:1 [▶]
    ]
  ]
]

So everything is basically in alphabetical order. Is there any way to change this so the year starts with newest to oldest, and the months are in calendar order?

Thanks

  • 写回答

2条回答 默认 最新

  • dqed19166 2018-03-26 15:53
    关注

    You can use uksort() to check manually:

    • if the keys is a numeric value: sort naturally
    • if the keys doesn't match with strtotime() sort with strcmp() (It could be better to check if the key is equal to a "predefined" month name instead of checking strtotime() === false.)
    • else sort using strtotime()

    Code:

    function sortArray($arr) {
        uksort($arr, function($k1, $k2) {
            if (is_numeric($k1)) return $k1-$k2 ;
            if (strtotime($k1) === false) return strcmp($k1, $k2);
            return strtotime($k1) - strtotime($k2);
        });
    
        foreach ($arr as $k => $v) {
            if (is_array($v)) {
                $arr[$k] = $this->sortArray($v);
            }
        }
        return $arr;
    }
    $array = sortArray($array);
    print_r($array);
    

    Outputs:

    Array (
      [SomeTitle] => Array (
        [2017] => Array (
          [January] => Array()
          [February] => Array()
          [March] => Array()
          [April] => Array()
          [May] => Array()
          [June] => Array()
          [July] => Array()
          [August] => Array()
          [September] => Array()
          [October] => Array()
          [December] => Array()
        )
        [2018] => Array (
          [January] => Array()
          [February] => Array()
          [March] => Array()
        )
      )
    )
    

    Here is a working demonstration.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看