1

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());
6
  • Use Session::put() instead of Session::push(). That should replace the session value. Commented Mar 24, 2021 at 18:23
  • i used PUT before but I dont want to replace , if diff values come i want new array in multi array Commented Mar 24, 2021 at 18:26
  • If you're looking to prevent duplicate values in a multi-dimensional array, you'll have to loop through the existing data and check it. Commented Mar 24, 2021 at 18:26
  • @Phaelaxz , foreach( Session::get("preBookDataToCompare") as $item => $key) { if( $key['user_id'] == $user_id && $key['item_id'] == $item_id && $key['start_date'] == $start_date->toDateString() && $key['end_date'] == $end_date->toDateString() ) // how to overwrite ['preBookHash'] in that array ? else // Session::push('preBookDataToCompare', $preBookDataToCompare); // but this dont prevent doubles :() } // how to modify that to work for me ????? Commented Mar 24, 2021 at 18:30
  • With each element in the array being an array of 5 values, what determines a duplicate? If only 1 value of the 5 matches? or only if all 5 values match? Or can we just look at the bookHash? If we can look at just that hash for duplicates, use that as a key in the parent array. Then you can use array_key_exists() Commented Mar 24, 2021 at 18:50

1 Answer 1

2

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());
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you , i modified a little bit as needed additional overwrite existing hash. I already handle problem with pure PHP , but definitly i prefer ur method and i have learn something new :)

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.