gpt4 book ai didi

php - 如何从 Laravel session 中删除数组

转载 作者:行者123 更新时间:2023-11-29 15:41:00 24 4
gpt4 key购买 nike

我正在 Laravel 中开发一个购物车,我想在 session 中删除数组,但它不起作用。

我已经尝试过这些:

https://www.allphptricks.com/simple-shopping-cart-using-php-and-mysql/
https://laracasts.com/discuss/channels/laravel/remove-array-element-from-session?page=1

但是没有成功

foreach($request->session()->get('shopping_cart') as $key => $value){
if($value['code'] == $request->remove_id){
$request->session()->forget("shopping_cart.". $request->remove_id);
break;
}
}
array:4 [▼
"fh5hhr34" => array:5 [▼
"name" => "Wilma Goodman"
"code" => "fh5hhr34"
"price" => "412"
"quantity" => "2"
"image" => "product_pics/1566372402.jpeg"
]
"ghtzzr" => array:5 [▼
"name" => "Lillian Hays"
"code" => "ghtzzr"
"price" => "187"
"quantity" => "4"
"image" => "product_pics/1566372214.jpg"
]
]

最佳答案

试试这个代码:

foreach(session('shopping_cart') as $key => $value){
if($value['code'] == $request->remove_id){
session()->forget("shopping_cart.$key");
break;
}
}

如果仍然不起作用,您可以尝试在 foreach 末尾保存 session ,即:

foreach(session('shopping_cart') as $key => $value){
//....
}
session()->save();

如果有效,请告诉我。

关于php - 如何从 Laravel session 中删除数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57661180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com