Forum Moderators: coopster & phranque

Message Too Old, No Replies

using unset() in multi-dimensional arrays

What's the best way to clear an item from my cart?

         

rycrostud

10:46 am on Dec 11, 2001 (gmt 0)

10+ Year Member



I'm building a shopping cart-type system and I'm having problems with the function that will delete individual line items from the cart.

I'm trying to use unset() with little success.

My cart array has this kind of format - a 2 dimensional array:

$cart[] = ($item = array(id => $id, qty => $qty, date_required => $date));

It's a bit more complex than a standard cart so I can't just store the product id and quantity to the array as a $key => $value pair. I also need to store variables such as the date the item is required and other user-selected options.

So my question is, what's the best method of deleting an $item[] from my $cart[]?

I've tried looping through each $cart[$row] unsing a foreach statement, using unset($cart[row]) when it needs to be deleted but it doesn't seem to be clearing the $item correctly.

I'm kinda new to this so I'm sure there's a much better way of doing it.

Any help will be appreciated.

Also, could one of the moderators please correct my awful typos in the topic title? Thanks,

sugarkane

7:59 pm on Dec 12, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you say that unset isn't clearing properly, what exactly happens?

How are you storing the cart contents from page to page? I tend to use a single delimited string which I decode/rebuild on every call of the cart script, so if I simply set the quantity field to zero and tell the rebuild code to ignore items with zero quantity, the item is deleted in that way.

Maybe you could work a similar idea into your code?