dongrong5189 2016-05-04 10:58
浏览 101
已采纳

如何更改会话数组变量值

I have an array of 'products' in my session, a product is an array of name, code and quantity, I want to change the quantity when I press a 'qty_up' button:

my PHP is this:

if ($_POST['qty_up']==''){
    foreach ($_SESSION["products"] as $key => $val)
    {
        if ($val["product_code"] == $_POST['code']) {
            $val["product_qty"] += 1;
        }
    }
}

This changes $val["product_qty"] but not the real value in the session

This is my 'products' array in the session:

array (size=1)
  'products' => 
    array (size=5)
      213453 => 
        array (size=5)
          'product_qty' => string '1' (length=1)
          'product_code' => string '213453' (length=6)
          'product_name' => string 'Kingfisher' (length=10)
          'product_price' => string '12.00' (length=5)
      48754 => 
        array (size=5)
          'product_qty' => string '1' (length=1)
          'product_code' => string '48754' (length=5)
          'product_name' => string 'Minute maid' (length=11)
          'product_price' => string '2.00' (length=4)
      '3545231ES0' => 
        array (size=5)
          'product_qty' => string '1' (length=1)
          'product_code' => string '3545231ES0' (length=10)
          'product_name' => string 'Jagurt' (length=6)
          'product_price' => string '1.00' (length=4)
  • 写回答

5条回答 默认 最新

  • dsy19890123 2016-05-04 11:02
    关注

    What's the need of this $val? You could directly update the session value.

    if ($_POST['qty_up']=='') {
    
       foreach ($_SESSION["products"] as $key => &$val) {
    
           if ($val["product_code"] == $_POST['code']) {
               //$val["product_qty"] += $val["product_qty"];
               $_SESSION["products"][$key]['product_qty'] +=  $val["product_qty"]; // Add this
           }
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!