I'm having a little issue, when I do this:
$_SESSION['cartItems'] = array();
It works fine, and it creates a cartItems array in the session.
But when I do this:
$_SESSION[2]['cartItems'] = array();
This works until I refresh the page, then it forgets this session array?
I tried to create the 2 array first:
$_SESSION[2] = array();
$_SESSION[2]['cartItems'] = array();
But still does not work like it is supposed to do.
How can I solve this?
Numeric keys are converted to strings because the $_SESSION
variable is an associative array. You might want to try using $_SESSION["2"]
when assigning or retrieving values.
Further more, it looks to me like you are trying to persist several cart arrays, so why not use something like this instead:
$_SESSION['carts'] = array();
$_SESSION['carts'][0] = array();
$_SESSION['carts'][1] = array();
...
Or even $_SESSION['carts'][0]['cartItems'] = array()
Some related posts for further reading:
I suggest you to use as serialized object.
$_SESSION['carts'] = serialize(array(0=> "foo", 1=> array("bar")));
In this case you can store complex data and it will be secure & clean.