I always find it interesting that people tend to want to complicate the uncomplicated. In this case, why you would want to create an array of values as a concatenated string and then convert it back to an array every time you want to use it.
If you create an array in your session (as a multi-dimensional array) it gets serialized automatically between page views, so what you are doing is done by default. Except that not doing it manually means that you don't have to manually undo it to use it.
So consider a cart item as a single dimensional array, as you have done in your code.
You're only carrying the item id in that array, so it's a bit limited as to being able to tell you how many you have of them. So, a multi-dimensional array ...
Code: Select all
$cart_item = array('id' = 4, 'qty'=>1);
Ok .. so we can have multiple variables for one item. But we want more than one item.
Code: Select all
$cart_item = array('id' = 4, 'qty'=>1);
$cart[] = $cart_item;
// which would allow use to go right on into
$cart_item = array('id' = 6, 'qty'=>4);
$cart[] = $cart_item;
// which would give us $cart containing two arrays, each containing the id and quantity of product.
But, we want to piggyback that onto the session, so ...
Code: Select all
$cart_item = array('id' = 4, 'qty'=>1);
$_SESSION['CART'][] = $cart_item;
So for each time you pass the second line above, a new cart id is created containing an array if the item data added. Best of all, this data is totally accessible and ready for use at any time.
If this looks like it might be of help, take a look at
THIS POST to get an idea of how to work with cart data in the proposed format.
Hope this helps though I didn't really directly answer your question.