我正在使用Google Calendar API从多个日历中提取数据。我正在创建一个数组,所以我可以格式化数据的显示。我在排序数据时遇到问题,因此事件将以正确的顺序显示。
我的主要排序是datetime
ASC。如果两个日期时间相等,我想对alldayflag
DESC进行排序。我只希望它在每个日期内排序。
以下是我的数据示例:
Array
(
[2016-01-29] => Array
(
[0] => Array
(
[date] => January 29
[time] => 8:30 am
[datetime] => 2016-01-29T08:30:00-06:00
[alldayflag] => 0
)
[1] => Array
(
[date] => January 29
[time] => 12:00 am
[datetime] => 2016-01-29T00:00:00-06:00
[alldayflag] => 1
)
[2] => Array
(
[date] => January 29
[time] => 2:00 pm
[datetime] => 2016-01-29T14:00:00-06:00
[alldayflag] => 0
)
[3] => Array
(
[date] => January 29
[time] => 10:00 am
[datetime] => 2016-01-29T10:00:00-06:00
[alldayflag] => 0
)
[4] => Array
(
[date] => January 29
[time] => 12:00 pm
[datetime] => 2016-01-29T12:00:00-06:00
[alldayflag] => 0
)
)
[2016-01-30] => Array
(
[0] => Array
(
[date] => January 30
[time] => 4:00 pm
[datetime] => 2016-01-30T16:00:00-06:00
[alldayflag] => 0
)
[1] => Array
(
[date] => January 30
[time] => 5:00 pm
[datetime] => 2016-01-30T17:00:00-06:00
[alldayflag] => 0
)
[2] => Array
(
[date] => January 30
[time] => 11:00 am
[datetime] => 2016-01-30T11:00:00-06:00
[alldayflag] => 0
)
)
)
我尝试过使用array_multisort()
。我得到了正确的排序结果,但我也收到了错误消息:
警告:array_multisort():第XXX行的sort-array.php中的数组大小不一致
$getBeginDate = '2016-01-29';
$getEndDate = '2016-01-31';
$getCurrentDate = $getBeginDate;
while(strtotime($getCurrentDate) < strtotime($getEndDate)) {
foreach ($list[$getCurrentDate] as $key => $row){
$datetime[$key] = $row['datetime'];
$alldayflag[$key] = $row['alldayflag'];
}
array_multisort($datetime, SORT_ASC, $alldayflag, SORT_DESC, $list[$getCurrentDate]);
$getCurrentDate = date('Y-m-d', strtotime($getCurrentDate . " +1 day"));
}
我也试过uasort()
。它根本没有正确排序。
uasort($list, 'sortCriteria');
function sortCriteria($array, $key) {
if(strtotime($a['datetime']) == strtotime($b['datetime'])) {
if($a['allday'] < $b['allday']) {
return -1;
} else {
return 0;
}
}
return (strtotime($a['datetime']) < strtotime($b['datetime'])) ? -1 : 1;
}
非常感谢任何帮助。
答案 0 :(得分:0)
array_multisort
绝对是你想去的方式。我有一个未经测试的答案,如果这是一个不正确的猜测,我可能会被社区的愤怒所震撼,但看起来你遇到了关闭问题,或者更确切地说是缺乏关闭问题。这应该可以解决您的问题:
...
while(strtotime($getCurrentDate) < strtotime($getEndDate)) {
$datetime = [];
$alldayflag = [];
foreach ($list[$getCurrentDate] as $key => $row){
...
我从您提到的错误中猜测,如果您在多重排序之前转储$datetime
,则第一次会有5个项目,第二次会有5个项目。
first_dump => '29th', '29th', '29th', '29th', '29th'
second_dump => '30th', '30th', '30th', '29th', '29th'
或类似的东西。发生了什么是$ datetime在第二轮while循环中持续存在。这是第二次轰炸你的multisort,因为只有3个项而不是5个。
我希望这是有道理的。
答案 1 :(得分:0)
可能的解决方案是使用usort。 然后,您可以使用DateTime来比较“日期时间”。 然后,如果两个日期时间相等,则可以比较'alldayflag'。
例如:
function sortCriteria($a, $b)
{
$aDateTime = new DateTime($a['datetime']);
$bDateTime = new DateTime($b['datetime']);
if ($aDateTime == $bDateTime) {
return ($a['alldayflag'] > $b['alldayflag']) ? -1 : 1;
}
return ($aDateTime < $bDateTime) ? -1 : 1;
}
$getBeginDate = '2016-01-29';
$getEndDate = '2016-01-31';
$getCurrentDate = $getBeginDate;
while(strtotime($getCurrentDate) < strtotime($getEndDate)) {
usort($list[$getCurrentDate], 'sortCriteria');
$getCurrentDate = date('Y-m-d', strtotime($getCurrentDate . " +1 day"));
}