在我的php foreach循环中,最后一个数据未显示我的表
我的功能
/**
* @param $totals
* @return mixed
*/
public function getCustomHour($totals)
{
$startMonth = date("Y/m/", $this->startDate);
$endMonth = date("Y/m/", $this->endDate);
$data = array();
$maxload = array();
$total = 0;
$hour = 0;
$day = 0;
if ($totals) {
foreach ($totals as $key => $testData) {
$today = $testData['day'];
$hourTotal = $testData['count'];
if ($day == 0) {
$day = $today;
}
if ($today == $day) {
if ($hourTotal > $total) {
$hour = $testData['hour'];
$total = $hourTotal;
}
} else {
if ($day < 10) {
$maxload["Day"] = $endMonth . "0" . $day;
} else {
$maxload["Day"] = $startMonth . $day;
}
$maxload["Hour"] = $hour;
$maxload["Count"] = $total;
$data[] = $maxload;
$day = $testData['day'];
$hour = 0;
$total = 0;
}
}
}
return $data;
}
我不知道此功能的工作原理,因为它是先前编写的,但据我所知,在下面的回复中提到
Day Hour Count
1 00 11
1 01 10
1 02 13
1 03 18
1 04 18
1 05 29
1 06 25
1 07 13
1 08 23
1 09 31
1 10 30
1 11 30
1 12 22
1 13 21
1 14 24
1 15 29
1 16 29
1 17 41
1 18 13
1 19 16
1 20 19
1 21 18
1 22 8
1 23 9
3 00 7
3 01 18
3 02 13
3 03 23
3 04 17
3 05 24
3 06 21
3 07 15
3 08 18
3 09 31
3 10 35
3 11 28
3 12 23
3 13 21
3 14 23
3 15 31
3 16 30
3 17 32
3 18 25
3 19 17
3 20 11
3 21 17
3 22 14
3 23 6
4 00 9
4 01 13
4 02 11
4 03 19
4 04 19
4 05 24
4 06 22
4 07 24
4 08 17
4 09 31
4 10 33
4 11 25
4 12 26
4 13 21
4 14 26
4 15 25
4 16 27
4 17 33
4 18 25
4 19 22
4 20 21
4 21 13
4 22 11
4 23 3
6 00 8
6 01 17
6 02 12
6 03 21
6 04 21
6 05 18
6 06 22
6 07 20
6 08 28
6 09 29
6 10 26
6 11 28
6 12 23
6 13 21
6 14 25
6 15 25
6 16 31
6 17 37
6 18 20
6 19 18
6 20 24
6 21 14
6 22 10
6 23 2
7 00 9
7 01 16
7 02 17
7 03 14
7 04 19
7 05 16
7 06 22
7 07 19
7 08 21
7 09 31
7 10 36
7 11 28
7 12 25
7 13 21
7 14 24
7 15 31
7 16 34
7 17 27
7 18 15
7 19 26
7 20 13
7 21 19
7 22 10
7 23 7
8 00 9
8 01 10
8 02 15
8 03 20
8 04 25
8 05 20
8 06 15
8 07 21
8 08 24
8 09 40
8 10 27
8 11 26
8 12 21
8 13 21
8 14 25
8 15 27
8 16 28
8 17 38
8 18 12
8 19 29
8 20 12
8 21 19
8 22 11
8 23 5
9 00 10
9 01 16
9 02 10
9 03 18
9 04 21
9 05 19
9 06 19
9 07 21
9 08 22
9 09 35
9 10 29
9 11 28
9 12 25
9 13 21
9 14 23
9 15 25
9 16 35
9 17 32
9 18 19
9 19 19
9 20 24
9 21 12
9 22 11
9 23 6
它将检查每天的最高计数,并返回该天的值。
所以我的问题是这里一切正常,但最后一次表示第9天数据没有显示
即使我尝试在我的else中打印日期,也只能打印134678,一旦输入else循环,它将跳过最后一个日期
我的输出
Day Hour Count
2019/01/01 17 41
2019/01/03 10 35
2019/01/04 10 33
2019/01/06 17 37
2019/01/07 10 36
2019/01/08 09 40
我不明白是什么问题?
答案 0 :(得分:0)
我编写了一种方法,该方法应该每天返回最高计数:
function getCustomHour($totals) {
$startMonth = date("Y/m/", $this->startDate);
$endMonth = date("Y/m/", $this->endDate);
$data = array();
$maxCount = array();
if (empty($totals)) {
return $data;
}
foreach ($totals as $key => $testData) {
$today = $testData['day'];
$count = $testData['count'];
if (empty($maxCount[$today]) || $count > $maxCount[$today]) {
$maxCount[$today] = $count;
$data[$today]['Count'] = $maxCount[$today];
$data[$today]['Hour'] = $testData['hour'];
if ($today < 10) {
$data[$today]["Day"] = $endMonth . "0" . $today;
} else {
$data[$today]["Day"] = $startMonth . $today;
}
}
}
return $data;
}