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条)

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝