doushe8577 2019-01-22 06:32
浏览 59
已采纳

php foreach最后的数据无法打印

In my php foreach loop last data not showing my table

my function

/**
 * @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;
}

i don't know how this function is working because it written previousely but as of my understand in response below mention

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

it will check each day count which is highest per day it will return that day values.

So my problem is here everything is working fine but last which means 9th day data not showing

even i try to print day in my else it only print 134678 once enter else loop it will skip last date

my output

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

i don't understand what is the problem ?

展开全部

  • 写回答

1条回答 默认 最新

  • dongqiao8417 2019-01-22 06:41
    关注

    I have written a method that should return highest count per day:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部