Laravel 会话中的多维数组,查找,覆盖
Laravel multidimensional array in session , finding , overwriting
我想在会话中保留多维数组:
$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();
会话::推送('preBookDataToCompare', $preBookDataToCompare);
数组在用户打开页面后立即生成并添加到会话中。
他可能会用 diff item_id 或 dates 打开新的 TAB ...
那么如何在会话中检查该数组:
- 如果设置:user_id && item_id && start_date && end_date 存在 ?
- 如果不存在,如何使用这些新值向该多数组添加新数组?
- 如果 YES 存在,如何只覆盖数组中所选数组的 ['preBookHash']?
dd(Session::get("preBookDataToCompare"));
显示:
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());
您可以使用laravel的收集方法contains()
,它可以关闭。您也可以执行 foreach 并为此使用普通的 PHP 。我更喜欢集合,这样你就不必检查会话值是否为空。
<?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());
}
我想在会话中保留多维数组:
$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();
会话::推送('preBookDataToCompare', $preBookDataToCompare);
数组在用户打开页面后立即生成并添加到会话中。 他可能会用 diff item_id 或 dates 打开新的 TAB ... 那么如何在会话中检查该数组:
- 如果设置:user_id && item_id && start_date && end_date 存在 ?
- 如果不存在,如何使用这些新值向该多数组添加新数组?
- 如果 YES 存在,如何只覆盖数组中所选数组的 ['preBookHash']?
dd(Session::get("preBookDataToCompare"));
显示:
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());
您可以使用laravel的收集方法contains()
,它可以关闭。您也可以执行 foreach 并为此使用普通的 PHP 。我更喜欢集合,这样你就不必检查会话值是否为空。
<?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());
}