0

i have session variable associated with user the first time the following code without the second line is execute it works on second time it does not giving me undefined offset.

unset($_SESSION['cart_items'][$i]);

this line of code solve the problem but my head is not getting around it

 $_SESSION["cart_items"] = array_values($_SESSION["cart_items"]);

1 Answers1

2

The unset() function removes the variable from scope and deletes the data, but it does not re-index the array, so you are left with an array that does not have an index of that number. array_values() just re-indexes the array so it is in order.

HenryTK
  • 1,287
  • 8
  • 11