简体   繁体   中英

Unable to unset an item from Laravel session array

I am trying to use unset/array_splice to remove an index of an item from an array, in the context of Laravel sessions.

What I have is:

  1. Check if class session key exists

  2. If so, get it, and loop through it

  3. If that particular index's element (a string) matches the input string (from an AJAX request), unset it, and put the new unsetted array BACK to the session

This operation, in theory (story of my life), should remove an item from the cart.

// Remove the item [if exists]
if(Session::has('class')) {
    $classes = Session::get('class');
    foreach($classes as $index => $class) {
        if($data['class'] === $class) {
            array_splice($classes, $index, $index - 1);
            Session::put('class', $classes);

            return Response::json(array(
                    'success' => true,
                    'code' => 1,
                    'message' => $data['class'] . ' removed from cart'
                )
            );
        }
    }
}

The data within the class session looks like this:

[
   "ECEC 471 Introduction to VLSI Design Lab",
   "ECEC 471 Introduction to VLSI Design Lecture",
   "ECEC 413 Introduction to Parallel Computer Architecture Lecture",
   "ECEC 457 Security in Computing Lecture & Recitation"
] 

I traced the code logically a couple times, but it does not unset the item. I know the string is matched, since my JSON response from the shown query is returned.

EDIT:

Ok, so I made a little breakthrough.

  1. I forgot to assign the key to put in the session, so I've changed it to this: Session::put('class', $data)

  2. Next, due to how the indices things work with array_splice , the unsetting works for all indices greater than 1. If the current index you want to unset is 0 or 1 , it fails, since respectively, the "new" index turns to -1 and 0 ..

As suggested, I used unset and array_values from this thread :

    // Remove the item [if exists]
    if(Session::has('class')) {
        $classes = Session::get('class');
        foreach($classes as $index => $class) {
            if($data['class'] === $class) {
                unset($classes[$index]);
                $newClass = array_values($classes);
                Session::put('class', $newClass);
                return Response::json(array(
                        'success' => true,
                        'code' => 1,
                        'class' => $classes,
                        'message' => $data['class'] . ' removed from cart'
                    )
                );
            }
        }
    }

What's happening is, when you use unset , it removes that particular index, but it doesn't preserve the index, so if you're removing the 1st index, the array indices would like look this: 0 2 3 4 . array_values will re-index everything, and life is good again.

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