【发布时间】:2019-11-01 15:27:00
【问题描述】:
我有 $_SESSION['products'] 来保存多个购物车项目,我希望总数量显示在标题上
我尝试使用以下脚本获取总量:
if(isset($_SESSION['products'])) {
$totalQty = 0;
foreach($_SESSION['products'] as $itemQty){
$totalQty += $itemQty;
}
}
echo $totalquantity;
我print_r($_SESSION['products'])在下面的购物车中获取当前商品:
Array
(
[0] => Array
(
[p_id] => 31
[p_name] => Product 31
[p_price] => 28.80
[p_qty] => 2
)
[1] => Array
(
[p_id] => 46
[p_name] => Product 46
[p_price] => 18.00
[p_qty] => 3
)
[2] => Array
(
[p_id] => 12
[p_name] => Product 12
[p_price] => 63.00
[p_qty] => 1
)
)
如何遍历 $_SESSION 并动态获取数组中的总 [p_qty]?
【问题讨论】: