我希望将$ total输出为一个数字的总和,但是每次我总结$ total时,它都会输出多个数字$total =0 // i tried to use a number to sum them up$computers = array("computer;DT-12;568,36;10", "Samsung; RS; 562,26;11", "Hewlett Packard; F12; 450,23; 23","Toshiba; LO-34; 454,23;8", "Sony; Vaio 123; 232,23;5");foreach($computers as $value){$product= explode(";",$value); $price = $product[2]; $count = $product[3]; $multiply = $count * $price;$total += $multiply}预期输出:$total = 27004 as one number
1 回答
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
您的代码中有一些语法错误。除此之外,您的计算看起来还不错,并且可以很好地完成工作explode();。
代码:
$computers = array(
"computer;DT-12;568,36;10",
"Samsung; RS; 562,26;11",
"Hewlett Packard; F12; 450,23; 23",
"Toshiba; LO-34; 454,23;8",
"Sony; Vaio 123; 232,23;5",
);
$total = 0;
foreach ($computers as $value) {
$product = explode(";", $value);
$total += (int) trim($product[2]) * (int) trim($product[3]);
}
var_dump($total);
输出:
您可能需要检查一下数学是否正确。
int(27004)
- 1 回答
- 0 关注
- 134 浏览
添加回答
举报
0/150
提交
取消