Foreach循环的总和

时间:2016-05-08 03:27:56

标签: php

我试图用我在PHP中工作的程序来计算账单余额。 我用来提取价格的代码就是这样。

> library("MASS", lib.loc="C:/Program Files/R/R-3.2.2/library")
Error in get(method, envir = home) : 
  lazy-load database 'C:/Program Files/R/R-3.2.2/library/MASS/R/MASS.rdb' is corrupt
In addition: Warning messages:
1: package ‘MASS’ was built under R version 3.2.5 
2: In get(method, envir = home) : internal error -3 in R_decompress1
Error: package or namespace load failed for ‘MASS’
> library(MASS)
Error in get(method, envir = home) : 
  lazy-load database 'C:/Program Files/R/R-3.2.2/library/MASS/R/MASS.rdb' is corrupt
In addition: Warning messages:
1: package ‘MASS’ was built under R version 3.2.5 
2: In .registerS3method(fin[i, 1], fin[i, 2], fin[i, 3], fin[i, 4],  :
  restarting interrupted promise evaluation
3: In get(method, envir = home) :
  restarting interrupted promise evaluation
4: In get(method, envir = home) : internal error -3 in R_decompress1
Error: package or namespace load failed for ‘MASS’

现在根据我的理解,这应该有效,但我反过来的是: enter image description here

在右侧,您可以看到结算总额,而不是将其全部单独给予我。

1 个答案:

答案 0 :(得分:1)

错误似乎很明显。用不同的词语表达你的序列:

  1. 获取所有包ID。
  2. Foreach包ID,获取商品的价格(只返回一个结果)
  3. 对于该单个结果,将所有价格相加(您只获得一个)。打印并返回2.
  4. 您看到的不是以某种方式添加为字符串的2个价格。您只需打印2种不同的价格。 10& 30

    GetPricing应该返回一个价格,并且调用它的foreach循环应该是总和。

    $total = 0;
    foreach($resource as $row) 
    {
        $total += self::GetPricing($row['packageID']);
    }
    

    希望这有帮助。