2

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..

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • To clarify, you want to remove a particular element from the array, and then renumber the indexes so that there are no 'missing' numbers in the order? If so, rather than array_splice, you could unset and then use array_values to re-index. – David Stromberg Aug 06 '15 at 20:17
  • Yes. I would like to preserve the indices. – theGreenCabbage Aug 06 '15 at 20:17
  • The weird issue I am having with `array_splice` is that for indices `0` and `1`, the element is not removed. Everything greater than 1 is able to be removed. I mean, I guess it makes logical sense. If I am removing the 0-th index item and the new array starts at the negative index, it doesn't make sense. – theGreenCabbage Aug 06 '15 at 20:19
  • If I am removing the 0-th index item and the new array starts at the negative index, obviously that wouldn't work.*** – theGreenCabbage Aug 06 '15 at 20:24

1 Answers1

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.

Community
  • 1
  • 1
theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169