doumor942473 2013-09-08 19:42
浏览 38
已采纳

在foreach结束之前的数组元素的总和,PHP图

Problem I'm having occured while building php script to automaticaly generate graph based on values from database.

I'm having all 12 months listed on X line and sum of monthly income for each month on Y line. In order to keep same size of graph everytime i want to convert sum of monthly income to % inpact on all graph, to do that i will use simple formula:

(array element / sum of all array elements) * 100

this should tell which % part of all graph is each array element

code is rather straight forward:

foreach($years as $year) {
// Do something
    foreach($months as $month) {
        foreach( // this foreach is getting monthly income foreach month \\ ) {
            // so in here we get $total_by_date[$year][$month];
        }
        $year_total[$year] += $total_by_date[$year][$month]; //sum of (sum of each month income)
        ?>
        <div style="background:black;height:20px;width:<?php echo ($total_by_date[$year][$month] / $year_total[$year]) * 100 ?>px;">div> //drawing graph based on given values
        <?php
    }
}

I checked it in various conditions and it appears to be working corectly, i got list of months foreach year, within months i got monthly income foreach month properly displayed. Still, essentialy its not working, in order it to fuly work i would need to get total income foreach year before i start outputing that draws graph, instead while each element from foreach loop is being scanned and displayed suming function is doing array walk so its incresing total value with each scanned month, at the end is giving few errors beacuse of january and february 0 income, even though year income should be 6700 - in few first months is displaying 2400, then 3500 then 5400 and finally 6700 at december.

i guess that i would need to get total year income before foreach starts with outputs month by month, the problem is i dont know how to do that. I came up with:

if(empty($year_sub[$year])) { unset($year_sub[$year]); continue; }

placed before div output, it helped a little, it removed all the initial 0's so it doesnt crash at the begining anymore, but still is doing sum on the fly which screws up the idea.

perhaps someone know how to start and finish suming before output or some other great solution. Thank You in advance.

  • 写回答

1条回答 默认 最新

  • dongshuo9350 2013-09-08 19:53
    关注

    You are printing the result before the total for the year is completed (code not tested).

    foreach($years as $year) {
    // Do something
        // 1. Sum of the year
        foreach($months as $month) {
            foreach( // this foreach is getting monthly income foreach month \\ ) {
                // so in here we get $total_by_date[$year][$month];
            }
            $year_total[$year] += $total_by_date[$year][$month]; //sum of (sum of each month income)
        }
        // 2. Convert to % and print
        foreach($months as $month) {
            ?>
            <div style="background:black;height:20px;width:<?php echo ($total_by_date[$year][$month] / $year_total[$year]) * 100 ?>px;">div> //drawing graph based on given values
            <?php
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败