简体   繁体   中英

How to Delete Single item or Array From Session Main Array

Controller: for get data from session

public function SessionDestroy(Request $request)
{
    if ($request->session()->has('data')) {
        return $request->session()->get('data');
    } else {
        return "No Data";
    }
}

this is session data i have and i want remove a single array:

[
    {
    "app_date": "2022-03-16",
    "department": "2",
    "doctor": "4",
    "fee": "150"
    },
    {
    "app_date": "2022-03-17",
    "department": "2",
    "doctor": "4",
    "fee": "150"
    },
    {
    "app_date": "2022-03-16",
    "department": "2",
    "doctor": "4",
    "fee": "150"
    }
]

So, How can i remove a single Array or Item

you don't have a unique id in your session to remove by this unique id..

this may help you !

public function SessionDestroy(Request $request)
{
    if (session()->has('data')) {
        session()->forget('data');
    if(!empty(request('app_date')) && is_array(request('app_date'))) 
        $new_data = [];
        $x = 0;
        foreach(request('app_date') as $app_date){
                $new_data[]['app_date'] = $app_date;
                $new_data[]['department'] = request('department')[$x]??'';
                $new_data[]['doctor'] = request('doctor')[$x]??'';
                $new_data[]['fee'] = request('fee')[$x]??'';
        }
        session()->put('data', $new_data);
        return session('data');
    } else {
        return "No Data";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM