我有一个温度列表,每n分钟记录一次。我想获取特定日期的最小ID和最大ID。 $listDate是数组中日期的列表。例如。

public getTemperature() {
    $listDate = ['2016-12-08','2016-12-09','2016-12-10','2016-12-11'];
    foreach($listDate as $date) {

        $getId = array();
        $getId[] = $this->selectRaw('MIN(`id`) as min, MAX(`id`) as max')
            ->where('created_at', '>=', $date. ' 00:00:00')
            ->where('created_at', '<=', $date. ' 23:59:59')
            ->get();
    }
    return $getId;
}


我希望它像这样的收藏回来

['min' => 1123, 'max' => 1345],
['min' => 1349, 'max' => 1567],
['min' => 1589, 'max' => 1612],
['min' => 1624, 'max' => 1655],

最佳答案

您只需要在循环外部定义数组:

public getTemperature() {
    $listDate = ['2016-12-08','2016-12-09','2016-12-10','2016-12-11'];

    $getId = []; //Define the array here

    foreach($listDate as $date) {
        $getId[] = $this->selectRaw('MIN(`id`) as min, MAX(`id`) as max')
            ->where('created_at', '>=', $date. ' 00:00:00')
            ->where('created_at', '<=', $date. ' 23:59:59')
            ->get();
    }
    return $getId;
}


由于如您的OP中所写,循环将在每次迭代中初始化数组。

希望这可以帮助。

关于php - Laravel Eloquent 结果收集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41181618/

10-11 08:29