Обновление предыдущего сеанса Array Laravel

У меня проблема с тем, как я могу обновить свой предыдущий массив? То, что в настоящее время происходит с моим кодом, – это просто добавление нового массива сеансов вместо обновления объявленного ключа, вот мой код:

foreach ($items_updated as $key => $added) { if ($id == $added['item_id']) { $newquantity = $added['item_quantity'] - 1; $update = array( 'item_id' => $items['item_id'], 'item_quantity' => $newquantity, ); } } Session::push('items', $updated); 

 $items = Session::get('items', []); foreach ($items as &$item) { if ($item['item_id'] == $id) { $item['item_quantity']--; } } Session::set('items', $items); 

Вы можете использовать Session :: forget ('key'); для удаления предыдущего массива в сеансе. И используйте Session :: push для добавления новых элементов.

Думаю, это сработает для вас, если вы на laravel 5.0. Но также обратите внимание, что я не тестировал его на laravel 4.x, однако я ожидаю такой же результат:

 //get the array of items (you will want to update) from the session variable $old_items = \Session::get('items'); //create a new array item with the index or key of the item //you will want to update, and make the changes you want to //make on the old item array index. //In this case I referred to the index or key as quantity to be //a bit explicit $new_item[$quantity] = $old_items[$quantity] - 1; //merge the new array with the old one to make the necessary update \Session::put('items',array_merge($old_items,$new_item));