Skip to content
Advertisement

Laravel multidimensional array in session , finding , overwriting

I would like to keep multidimentional array in session :

    $preBookDataToCompare = [];
    $preBookDataToCompare['preBookHash'] = $this->hashString($dataToHash);
    $preBookDataToCompare['user_id'] = $user_id;
    $preBookDataToCompare['item_id'] = $item_id;
    $preBookDataToCompare['start_date'] = $start_date->toDateString();
    $preBookDataToCompare['end_date'] = $end_date->toDateString();

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 :

  0 => array:5 [▼
    "preBookHash" => "c900cf028634f1d00005c6aad98be406"
    "user_id" => "a9eac834-7e11-4892-accd-50cd2386c3cc"
    "item_id" => "4a69e17e-7023-4a9d-9c6b-25bc73b7e0c3"
    "start_date" => "2021-03-25"
    "end_date" => "2021-03-31"
  ]
  1 => array:5 [▼
    "preBookHash" => "62bf5756b311b2d2b1cada8d9fe997e2"
    "user_id" => "a9eac834-7e11-4892-accd-50cd2386c3cc"
    "item_id" => "21eeccd9-cc13-4ee7-8432-e7f83792cdfd"
    "start_date" => "2021-03-25"
    "end_date" => "2021-03-31"
  ]
  2 => array:5 [▼
    "preBookHash" => "c900cf028634f1d00005c6aad98be406"
    "user_id" => "a9eac834-7e11-4892-accd-50cd2386c3cc"
    "item_id" => "4a69e17e-7023-4a9d-9c6b-25bc73b7e0c3"
    "start_date" => "2021-03-25"
    "end_date" => "2021-03-31"
  ]
]```

[0] and [2] are dubled :(
Session::push('preBookDataToCompare', $preBookDataToCompare);
I know that PUSH is append new array.
How to make it checks if values(dataset) exists , if yes overwrite , if not add new one , but prevent dubbles. ?

--------------------------------------------------------
WORKING SOLUTION : (thx to : Mihir Bhende )

        $preBookDataToCompare = [];
        $preBookDataToCompare['preBookHash'] = $this->hashString($dataToHash);
        $preBookDataToCompare['user_id'] = $user_id;
        $preBookDataToCompare['item_id'] = $item_id;
        $preBookDataToCompare['start_date'] = $start_date->toDateString();
        $preBookDataToCompare['end_date'] = $end_date->toDateString();

        $newItem = [
            'user_id' => $user_id,
            'item_id' => $item_id,
            'start_date' => $start_date->toDateString(),
            'end_date' => $end_date->toDateString()
        ];
        $newHash = $this->hashString($dataToHash);

        // Get existing session data
        $sessionData = collect(Session::get('preBookDataToCompare'));

        // Check if new item already exists matching key value pairs
        $alreadyExists = $sessionData->contains(function($value) use($newItem, $newHash){
            $newItem['preBookHash'] = $newHash ;
            return $newItem == $value;
        });

        // Add if item does not exist
        if(!$alreadyExists){
            $newItem['preBookHash'] = $this->hashString($dataToHash);
            $sessionData->push($newItem);
        }

        Session::put('preBookDataToCompare', $sessionData->toArray());

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.

<?php 

$newItem = [
    'someKey' => 'somevalue',
    'otherkey' => 'othervalue'
];
// Get existing session data
$sessionData = collect($session->get('preBookDataToCompare'));

// Check if new item already exists matching key value pairs
$alreadyExists = $sessionData->contains(function($value) use($newItem){
    
    return $newItem == $value;
});

// Add if item does not exist
if(!$alreadyExists){
    $sessionData->push($newItem);
    session()->put('preBookDataToCompare', $sessionData->toArray());
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement