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.

图片转代码服务由CSDN问答提供 功能建议

问题我在构建php脚本时发生了根据数据库中的值自动生成图形。

我在X线上列出了所有12个月的月份,以及Y线上每个月的月收入总和。 为了在每次我想要将月收入的总和转换为所有图形上的%inpact时保持相同大小的图形,为此我将使用简单的公式:

(数组元素/总和 所有数组元素)* 100

这应该告诉所有图的哪个%部分是每个数组元素

代码是相当直接的:

  foreach($ year as $ year){
 //做一些事情
 foreach($ month作为$ month){
 foreach(//这个foreach正在获得月收入预期 月\\){
 //所以在这里我们得到$ total_by_date [$ year] [$ month]; 
} 
 $ year_total [$ year] + = $ total_by_date [$ year] [$ month];  //(每月收入之和)之和
?&gt; 
&lt; div style =“background:black; height:20px; width:&lt;?php echo($ total_by_date [$ year] [$ month]  / $ year_total [$ year])* 100?&gt; px;“&gt; div&gt;  //基于给定值绘制图形
&lt;?php 
} 
} 
   
 
 

我在各种条件下检查它并且它似乎正在工作 ,我得到了一些月份的清单,在几个月内我得到了正确显示的月收入。 尽管如此,它本身并不起作用,为了使其工作,我需要在开始输出绘制图形之前得到总收入,而不是从foreach循环中的每个元素被扫描并显示求和函数正在进行数组行走所以它 每个扫描月份的总价值增加,最终在1月和2月0收入时几乎没有错误,即使年收入应该是6700 - 在前几个月显示2400,然后是3500然后是5400,最后是12月的6700。 / p>

我想我需要在foreach开始逐月输出之前得到总年收入,问题是我不知道该怎么做。 我想出了:

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

放在div输出之前,它有点帮助,它删除了所有的初始0,所以它不会在开始时崩溃,但仍然在做 这个想法搞砸了。

或许有人知道如何在输出或其他一些很好的解决方案之前开始和完成总结。 谢谢你提前。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题