我希望將$ total輸出為一個數(shù)字的總和,但是每次我總結(jié)$ total時,它都會輸出多個數(shù)字$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}預(yù)期輸出:$total = 27004 as one number
1 回答

拉風(fēng)的咖菲貓
TA貢獻(xiàn)1995條經(jīng)驗 獲得超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);
輸出:
您可能需要檢查一下數(shù)學(xué)是否正確。
int(27004)
- 1 回答
- 0 關(guān)注
- 150 瀏覽
添加回答
舉報
0/150
提交
取消