Page 1 of 1

deleting elements from array

Posted: Thu Jun 30, 2005 12:53 pm
by jestro
I am using an array to track a users shopping cart. My array know how is a little lackin, I know.
So I've got it working fine, except for deleting items. I can get it to delete the data with unset, but there is still an empty element within the array. I'd like empty elements to begone, and the numbers to re-index. I know this is over coding, and I should use a loop somehow, but I'm a newb.
Anyway heres the code

Code: Select all

unset($cart[$r]["style"]);
$r++;

unset($cart[$r]["color"]);
$r++;

unset($cart[$r]["size"]);
$r++;
$p = $cart[$r]["price_ea"];
unset($cart[$r]["price_ea"]);
$r++;

$num_items -= $cart[$r]["qty"];
$total_price -=$cart[$r]["qty"]*$p;

unset($cart[$r]["qty"]);
Thanks in advance!

Posted: Thu Jun 30, 2005 1:09 pm
by kendall
yo,

PHP has some pretty good array functions and maybe there is a function that can best suit your need

http://www.php.net/array

Posted: Thu Jun 30, 2005 1:16 pm
by jestro
Yeah, I've been reading through em, I just cant find the one to do what I want to do. Unset is the closest I can find.