Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Heimdall's avatar

Update session array

Hello,

I have a little problem :/ i want update my session array, but idk how

i have this : $productsessions = Session::get("products_session");

This return :


array:4 [
  0 => array:2 [
    "product_id" => 16
    "quantity" => 0
  ]
  1 => array:2 [
    "product_id" => 17
    "quantity" => 0
  ]
  2 => array:2 [
    "product_id" => 20
    "quantity" => 0
  ]
  3 => array:2 [
    "product_id" => 22
    "quantity" => 0
  ]
]

But on my controller i want for exemple update quantity when product_id = xx

i try this :


  foreach ($productsessions as $productsession) {
      if ($productsession['product_id'] == $product_id) {
          $productsession['quantity'] =  10;
      }
  }

but that dont work

Ty for your help

0 likes
2 replies
MichalOravec's avatar
$productsSession = Session::get('products_session', []);

foreach ($productsSession as $productSession) {
    if ($productSession['product_id'] == $product_id) {
        $productSession['quantity'] =  10;
    }
}

Session::set('products_session', $productsSession);

You can also use session helper

$productsSession = session('products_session', []);

session(['products_session' => $productsSession]);

You have to again save session if you want to update it.

Please or to participate in this conversation.