5

I have an issue on how can I update my Previous array ? What currently happening to my code is its just adding new session array instead of updating the declared key here's my code:

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);
1
  • I'm not sure as what you are trying to do but I can see that when you are looping, you will just be overriding the content of the array $update, I would suggest to use array_push; perhaps something like $update[] Commented Jul 15, 2014 at 18:47

4 Answers 4

11
$items = Session::get('items', []);

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

Session::set('items', $items);
Sign up to request clarification or add additional context in comments.

2 Comments

It works Thank you Joseph!!! but what does $items as &$item ? I want to understand it thank you.
The ampersand means that it's a reference to the individual item array, not a copy. If you were to do $items as $item, the $item variable would hold a copy of the item in the $items array, and decreasing its item_quantity property would have no effect on the item in the $items array.
4

If you have nested arrays inside your session array. You can use the following way to update the session: $session()->put('user.age',$age);

Example

Supppose you have following array structure inside your session

$user = [
     "name" => "Joe",
     "age"  => 23
]

session()->put('user',$user);

//updating the age in session
session()->put('user.age',49);

if your session array is n-arrays deep then use the dot (.) followed by key names to reach to the nth value or array, like session->put('user.comments.likes',$likes)

Comments

0

I guess this will work for you if you are on laravel 5.0. But also note, that I haven't tested it on laravel 4.x, however, I expect the same result anyway:

//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));

Comments

-1

You can use Session::forget('key'); to remove the previous array in session.

And use Session::push to add new items to Session.

1 Comment

What would I need is saving again with its same Item_Id but with new Quantity so I think Session::forget('key') is not the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.