简体   繁体   中英

How to remove an item from laravel session by giving the index of the session array

Hello I'm new to Laravel I store family member details inside the session by using the below method and i want to delete family member by using the index of the session please someone help me.

session()->push('families',$request);

这就是我的数组的样子

This is to do it using PHP array_search function:

$families = session()->pull('families', []);
    if(($key = array_search($deleteID, $families)) !== false) {
        unset($families[$key]);
    }
session()->put('families', $families);
// PS: specify index you want to remove on $deleteID variable

or more simple way:

$index = 0; // let's say it's index 0
$families = Session::get('families'); // save the array
unset($families[$index]); // remove value from array based on index
Session::put('families', $families); // set the array again
// PS: specify index you want to remove on $index variable

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