2012-12-03 36 views
0

我正在使用表单在会话中创建几个数组。每次提交表单时都会创建一个新的_SESSION ['item'] []包含一个新数组。代码为:查找Session数组中所有键的总和

$newitem = array (
    'id' => $row_getshoppingcart['id'] , 
    'icon' => $row_getimages['icon'], 
    'title' => $row_getimages['title'], 
    'medium' => $row_getshoppingcart['medium'], 
    'size' => $row_getshoppingcart['size'], 
    'price' => $row_getshoppingcart['price'], 
    'shipping' => $row_getshoppingcart['shipping']); 

$_SESSION['item'][] = $newitem; 

根据用户提交表单的次数,可能有任意数量的项目数组。我如何从整个会话中的每个项目获取关键“价格”的总和并在页面上回显它?

预先感谢您的时间。对此,我真的非常感激。

回答

2

试试这个(未经测试):

$sum = 0; 
foreach ($_SESSION['item'] as $item) 
    $sum += $item['price']; 
echo $sum; 
+0

太好了。有用!非常感谢你的帮助。 –

1

循环通过他们:

$total = 0; 
foreach($_SESSION['item'] as $item) { 
    $total += $item['price']; 
} 
echo $total; 
+0

它的伟大工程,非常感谢你的帮助。 –