deleting elements from array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jestro
Forum Newbie
Posts: 2
Joined: Thu Jun 30, 2005 12:46 pm

deleting elements from array

Post 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!
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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
jestro
Forum Newbie
Posts: 2
Joined: Thu Jun 30, 2005 12:46 pm

Post 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.
Post Reply