0

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

2
Contributors
6
Replies
29
Views
4 Years
Discussion Span
Last Post by dhani09
0

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]);
    }
}
0

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.

0

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 ) )

0

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);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.