Update value in session array php

前端 未结 3 1507
小鲜肉
小鲜肉 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 14:52

    Store them as arrays, that way you can access the quantity using the ID as a key:

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

    So instead of storing your ID and Quantity in two separate strings, you have them in one array, with the ID as the key. Converting your example above your array will look like this:

    array(
        1    => 3
        4    => 4
        6    => 5
    );
    

    Then if you wanted to add / adjust anything you just set $id and $quantity to the appropriate values and use the line above.

提交回复
热议问题