I have an array that looks like this:
Array ( [0] => Array ( [id] => 2 [name] => test product1 [description] => test [quantity] => 1 [price] => 188 [status] => Brand New [category] => Computer Software [subcategory] => Music Software [postdate] => 2013-05-19 [prodimage] => [user] => admin ) [1] => Array ( [id] => 2 [name] => test product1 [description] => test [quantity] => 1 [price] => 188 [status] => Brand New [category] => Computer Software [subcategory] => Music Software [postdate] => 2013-05-19 [prodimage] => [user] => admin )
I want to be able to delete an entire row e.g Array[0] which would mean deleting this;
Array ( [0] => Array ( [id] => 2 [name] => test product1 [description] => test [quantity] => 1 [price] => 188 [status] => Brand New [category] => Computer Software [subcategory] => Music Software [postdate] => 2013-05-19 [prodimage] => [user] => admin ).
I tried the following:

$delid = $_GET['delid']//the product id of the element on which DELETE link was clicked.
if($delid){
    $contents = count($_SESSION['cartItems']);//$_SESSION['cartItems'] is the array shown above.
    for($i=0; $i < $contents $i++){
        foreach($_SESSION['cartItems'] as $products){
        if($products['id'] == $delid){
            unset($products[$i]);
           }
        }
    }
}

But it doesn't delete the specified item form the array. Never implemented such a thing before so i'm a bit confused. How do i go about this? thanks

Recommended Answers

All 6 Replies

Your array has two identical records (both [id] => 2), so I assume you copy/pasted for quickness.
You also have a ; missing in your for statement after $contents

foreach($_SESSION['cartItems'] as $key => $products){
    if($products['id']==$delid) {
        unset($_SESSION['cartItems'][$key]);
    }
}

THANKS. But the code doesn't work correctly. When i click an item it doesn't get deleted rather the item below it gets deleted. if there isn't an item below it, nothing happens.

Can you post a var_dump of your actual $_SESSION['cartItems'] ?

yea. This is what it looks like.
Array ( [0] => Array ( [id] => 18 [name] => book [description] => [quantity] => 0 [price] => 50 [status] => Brand New [category] => Books [subcategory] => Engineering [postdate] => 2013-05-21 [prodimage] => 1369144897sale.png [user] => admin ) [1] => Array ( [id] => 19 [name] => testing [description] => for testing [quantity] => 2 [price] => 182 [status] => Brand New [category] => Automobiles [subcategory] => Cars [postdate] => 2013-05-22 [prodimage] => [user] => admin ) [2] => Array ( [id] => 1 [name] => Fruity Loops [description] => dj mixer [quantity] => 1 [price] => 200 [status] => Brand New [category] => Computer Software [subcategory] => Music Software [postdate] => 2013-05-18 [prodimage] => [user] => user ) )

Code works fine, but in case I mistyped $_SESSION, here's the before and after delid=19 is removed.

$delid = 19;

$cartItems = array(
    array('id' => 18, 'name' => 'book'),
    array('id' => 19, 'name' => 'testing'),
    array('id' => 1, 'name' => 'fruity loops')
 );

 print_r($cartItems);

foreach($cartItems as $key => $products){
    if($products['id']==$delid) {
        unset($cartItems[$key]);
    }
}

echo '<hr>';
print_r($cartItems);

Works now!!!..
thanks

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.