【问题标题】:Find sum of all keys in Session array查找会话数组中所有键的总和
【发布时间】:2012-12-03 23:46:29
【问题描述】:

我正在使用表单在 Session 中创建多个数组。每次提交表单时,都会创建一个包含新数组的新 _SESSION['item'][]。代码:

$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;

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

提前感谢您抽出宝贵时间。我真的很感激。

【问题讨论】:

    标签: php arrays session key sum


    【解决方案1】:

    试试这个(未经测试):

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

    【讨论】:

      【解决方案2】:

      遍历它们:

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

      【讨论】:

        猜你喜欢
        • 2015-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-16
        • 2010-12-02
        • 2020-02-22
        • 1970-01-01
        • 2023-02-04
        相关资源
        最近更新 更多