I have a 2D array like this:

$cart = array(array("Item", 6.99), 
                     (array("Item 2", 13.99));

i am using this for a shopping cart i am making. I can add to it fine. What i need to be able to do is remove items from the array. I have been racking my brain trying to think of a way to do this and i've come up with nothing...

Recommended Answers

All 4 Replies

use the unset() function. Like so:

unset($array[index]);

this works when i remove the last item but if i try to remove anyothers the whole thing messes up. the items are not displayed but are still in an array. i cant explain it

In that case you might have to create a new array that excludes the element you want to delete, by slicing the original array (makes any sense)?

The php functions array_slice and array_merge should help.

simple.

<?php
$cart = array(array("Item", 6.99),(array("Item 2", 13.99)));  //$cart is a 2 dimensional array
print_r($cart); //print the contents of cart
unset($cart[0]); //unset 0th array element
print_r($cart); //print the cart
sort($cart); //sort the cart elements 
print_r($cart); //print sorted cart
?>

Thats it. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.