在排序第2级3维数组基于第三级值 [英] Sorting 3 dimensional array at 2nd level based on 3rd level values

查看:229
本文介绍了在排序第2级3维数组基于第三级值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是谷歌日历API将来自多个日历数据。我创建一个数组,所以我可以格式化数据的显示。我无法对数据进行排序,以便事件将出现在正确的顺序。

我的主排序是日期时间 ASC。如果这两个日期时间等于我要作为排序依据 alldayflag DESC。我只希望它整理每个日期之内。

下面是我的数据的样本:

 阵列

[2016年1月29日] =>排列
    (
        [0] =>排列
            (
                [日期] => 1月29日
                [时间] =>上午8点30
                [日期时间] => 2016-01-29T08:30:00-06:00
                [alldayflag] => 0
            )
        [1] =>排列
            (
                [日期] => 1月29日
                [时间] => 12:00 AM
                [日期时间] => 2016-01-29T00:00:00-06:00
                [alldayflag] => 1
            )
        [2] =>排列
            (
                [日期] => 1月29日
                [时间] =>下午2:00
                [日期时间] => 2016-01-29T14:00:00-06:00
                [alldayflag] => 0
            )
        [3] =>排列
            (
                [日期] => 1月29日
                [时间] => 10:00 AM
                [日期时间] => 2016-01-29T10:00:00-06:00
                [alldayflag] => 0
            )
        [4] =>排列
            (
                [日期] => 1月29日
                [时间] => 12:00 PM
                [日期时间] => 2016-01-29T12:00:00-06:00
                [alldayflag] => 0
            )
    )
[二零一六年一月三十日] =>排列
    (
        [0] =>排列
            (
                [日期] => 1月30日
                [时间] => 4:00 PM
                [日期时间] => 2016-01-30T16:00:00-06:00
                [alldayflag] => 0
            )
        [1] =>排列
            (
                [日期] => 1月30日
                [时间] => 5:00 PM
                [日期时间] => 2016-01-30T17:00:00-06:00
                [alldayflag] => 0
            )
        [2] =>排列
            (
                [日期] => 1月30日
                [时间] => 11:00 AM
                [日期时间] => 2016-01-30T11:00:00-06:00
                [alldayflag] => 0
            )
    )

我一直在使用在array_multisort试过()。我得到正确的排序结果,但是我也得到了错误消息:


  

警告:在array_multisort():数组的大小是在网上XXX排序array.php不一致


  $ getBeginDate ='2016年1月29日;
$ getEndDate ='2016年1月31日;$ getCurrentDate = $ getBeginDate;而(的strtotime($ getCurrentDate)LT;的strtotime($ getEndDate)){    的foreach($列表[$ getCurrentDate]为$关键=> $行){
         $日期时间[$关键] = $行['日期时间'];
         $ alldayflag [$关键] = $行['alldayflag'];
    }    在array_multisort($日期时间,SORT_ASC,$ alldayflag,SORT_DESC,$列表[$ getCurrentDate]);    $ getCurrentDate =日期('Y-M-D时的strtotime($ getCurrentDate+1天));}

我也试过 uasort()。它不正确排序的。

  uasort($列表中,sortCriteria');功能sortCriteria($数组$键){
    如果(的strtotime($ A ['日期时间'])==的strtotime($ B ['日期时间'])){
        如果($ A ['allday'< $ B ['allday']){
            返回-1;
        }其他{
            返回0;
        }
    }    回报(的strtotime($ A ['日期时间'])LT;的strtotime($ B ['日期时间']))? -1:1;}

任何帮助是极大AP preciated。


解决方案

在array_multisort 绝对是你想要去的方式。我对你有一个未经考验的答案,所以我可以通过,如果它是一个不正确的猜测社区的愤怒被smited,但它看起来像你运行到封闭的问题,或者说其缺乏。这应该可以解决您的问题:

  ...
而(的strtotime($ getCurrentDate)LT;的strtotime($ getEndDate)){$日期时间= [];
$ alldayflag = [];
的foreach($列表[$ getCurrentDate]为$关键=> $行){
...

这是你提到的错误我的猜测是,如果你甩了 $日期时间的multisort之前它会在它的第一次5个项目,并在其中5个项目第二时间。

  first_dump => 第29,29日,第29,29日,29日
second_dump => '30','30','30','29日,29日

或者类似这样的东西。这是怎么回事是$日期时间通过第一轮while循环进入第二次持续。因为只有3个项目,而不是5分轰炸了你multisort第二次了。

我希望是有道理的。

I am using the Google Calendar API to pull data from multiple calendars. I am creating an array so I can format the display of the data. I am having trouble sorting the data so events will appear in the proper order.

My primary sort is on datetime ASC. If the two datetimes are equal I want to sort on alldayflag DESC. I only want it sorted within each date.

Here is a sample of my data:

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 
            ) 
    ) 
)

I have tried using array_multisort(). I get the proper sorting results however I also get the error message:

Warning: array_multisort(): Array sizes are inconsistent in sort-array.php on line XXX

$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"));

}

I have also tried uasort(). It doesn't sort properly at all.

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;

}

Any help is greatly appreciated.

解决方案

array_multisort is definitely the way that you want to go. I have an untested answer for you, so may I be smited by the wrath of the community if it's an incorrect guess, but it looks like you're running into a closure issue, or rather the lack thereof. This should fix your issue:

...
while(strtotime($getCurrentDate) < strtotime($getEndDate)) {

$datetime = [];
$alldayflag = [];
foreach ($list[$getCurrentDate] as $key => $row){
...

My guess from the error you mentioned is that if you dumped $datetime before the multisort it would have 5 items in it the first time and 5 items in it the second time.

first_dump => '29th', '29th', '29th', '29th', '29th'
second_dump => '30th', '30th', '30th', '29th', '29th'

Or something similar to this. What's happening is $datetime is persisting through the first round of the while loop into the second time. Which is bombing out your multisort the second time because there are only 3 items and not 5.

I hope that makes sense.

这篇关于在排序第2级3维数组基于第三级值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆