首页 > 循环遍历 组合从数据库里查出来的数据,性能不好,怎么办?

循环遍历 组合从数据库里查出来的数据,性能不好,怎么办?

需求:根据开始、截止时间统计每天的数据
现在的从数据库查出来的数据大概有10个二维数组,结构相同 如下:

[
    '0' => [
        'time' => '2016-8-3',
        'data1'=> 'xxx',
        ...
    ]
]

因为每个数组中有需要的字段,所以要把这10个二维数组进行组合,思路如下:
根据日期遍历取数据

while (strtotime($start_time) < strtotime($end_time)) {
    
    // 10个foreach
    foreach($arr as $k => $v) {
        if (strtotime($start_time) == strtotime($v['time'])) {
            $data[] = $v[];
            ...
        }
    }
    
    foreach($arr as $k => $v) {
        if (strtotime($start_time) == strtotime($v['time'])) {
            $data[] = $v[];
            ...
        }
    }
    
    foreach($arr as $k => $v) {
        if (strtotime($start_time) == strtotime($v['time'])) {
            $data[] = $v[];
            ...
        }
    }
    ...

    $start_time = strtotime($start_time . ' +1 day');
    
}

最后想组合成的数据结构:

[
    '2016-8-3' => [
        'data' => '',
        ...
    ],
    
    '2016-8-4' => [
        'data' => '',
        ...
    ]
    ..
]

可是貌似那个while的性能很不好,跑不出数据,等待好久直接提示:
Maximum execution time of 30 seconds exceeded
各位大神有什么好的建议吗?


你的循环有问题,造成无尽循环了。你去打印下每次循环$start_time和$end_time到底是多少,肯定是strtotime($start_time) < strtotime($end_time)永远为真

【热门文章】
【热门文章】