我已经为此苦苦挣扎了一段时间,所以我决定寻求帮助。
我需要像这样转换日期列表:
2019-09-17,
2019-09-16,
2018-08-15,
2017-08-14
并将它们输出为唯一的年/月/日的嵌套列表:
$dates = wp_list_pluck( $queriedFiles->posts, 'post_date' );
$cleanDates = array();
foreach ($dates as $date) {
$createDate = new DateTime($date);
$date = $createDate->format('Y-m-d');
array_push($cleanDates, $date);
}
$uniqueDates = array_unique($cleanDates);
$years = array();
$properDates = array();
foreach ($uniqueDates as $date) {
$createDate = new DateTime($date);
$strippedYear = $createDate->format('Y');
$strippedMonth = $createDate->format('m');
$strippedDay = $createDate->format('d');
$thisDate = array('year' => $strippedYear, 'month'=>$strippedMonth, 'day'=>$strippedDay);
array_push($years, $strippedYear);
array_push($properDates, $thisDate);
}
我希望最终得到一个多维数组,我可以循环访问它以获得年/月/日
你们能提供的任何帮助都会很棒。谢谢!
答案 0 :(得分:1)
您可以使用sceneDidBecomeActive
。像这样:
ViewController
会产生
explode
如果要排序,您可以像这样简单地排序:
$dates = ['2019-09-17', '2019-09-16', '2018-08-15', '2017-08-14'];
$result = [];
foreach($dates as $date) {
$dateParts = explode('-', $date);
$result[$dateParts[0]][$dateParts[1]][] = $dateParts[2];
}
array(3) {
[2019]=>
array(1) {
["09"]=>
array(2) {
[0]=>
string(2) "17"
[1]=>
string(2) "16"
}
}
[2018]=>
array(1) {
["08"]=>
array(1) {
[0]=>
string(2) "15"
}
}
[2017]=>
array(1) {
["08"]=>
array(1) {
[0]=>
string(2) "14"
}
}
}
(升序)或foreach($result as &$e){
ksort($e);
foreach($e as &$t){
asort($t);
}
}
ksort($result);
(降序)将按键排序,而ksort
(升序)或krsort
(降序)将按值排序; >