我正在嘗試構建我自己的“購物車”系統,我想獲取所有 ['Price'] 列并將它們加在一起以獲得銷售總額。我試圖將它們推出 $variable 或 $_SESSION['total']; 所以我可以將它顯示給最終用戶。
array(3) {
[1]=>
array(4) {
["Qty"]=>
string(1) "1"
["Price"]=>
string(1) "1"
["Name"]=>
string(4) "Coke"
}
[2]=>
array(4) {
["Qty"]=>
string(1) "1"
["Price"]=>
string(1) "1"
["Name"]=>
string(4) "Coke"
}
[3]=>
array(4) {
["Qty"]=>
string(1) "1"
["Price"]=>
string(1) "1"
["Name"]=>
string(4) "Coke"
}
}
uj5u.com熱心網友回復:
您需要遍歷購物車陣列中的每個產品并將總價格添加到變數中。像這樣的東西:
$price_total = 0;
// If you need to take quantity into account
foreach ($order_product as $product) {
$price_total = $price_total ($product['Qty'] * $product['Price']);
}
// If you don't need to take quantity into account
foreach ($order_product as $product) {
$price_total = $price_total $product['Price'];
}
uj5u.com熱心網友回復:
假設您已經擁有與上面提到的相同的陣列:
$total = array_column( $array, 'Price' ); // taking the array and getting the values of the price
$total = array_sum( $total ); // add the values together to get the total price from it
// now you can put it in session too if you want
$_SESSION['CART_TOTAL_PRICE'] = $total;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/373475.html
