Update value in session array php

前端 未结 3 1504
小鲜肉
小鲜肉 2021-01-07 14:33

I have question about the SESSION array.

I just add item and qty in different session. I use code like this :

$_SESSION[\'idproduct\'] = $id.\",\";         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 15:14

    The best way to achieve it is to store quantity as a value with product ID as a key, so if you have:

    idproduct  = 1,4,6,
    qtyproduct = 3,4,5,
    

    Store it as:

    $_SESSION['qtyproduct'] = array(
      1 => 3,
      4 => 4,
      6 => 5,
    );
    

    Right now if you have a product id:

    $id = 4;
    

    You can access quantity with:

    $quantity = $_SESSION['qtyproduct'][$id];
    

    and modify it with:

    $_SESSION['qtyproduct'][$id] = 7;
    

提交回复
热议问题