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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?