Skip to content
Advertisement

Laravel multidimensional array in session , finding , overwriting

I would like to keep multidimentional array in session :

JavaScript

Session::push(‘preBookDataToCompare’, $preBookDataToCompare);

Array is generated and added to session just after user open page. He may open new TAB with diff item_id or dates … So how to check that array in session for :

  1. if set : user_id && item_id && start_date && end_date EXIST ?
  2. if NOT exists how to add new array to that multi array with these new values ?
  3. if YES exists how to overwrite only [‘preBookHash’] for that selected array in array ?

dd(Session::get("preBookDataToCompare")); shows :

JavaScript

Advertisement

Answer

You can use laravel’s collection method called contains() which can get closure. You can also do foreach and use plain PHP for this. I prefer collection so you don’t have to check if session value is empty.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement