removing items from multidimensional array

Discussion in 'PHP' started by vassili, Feb 13, 2008.

  1. #1
    i have this array $cartSizes[$productid] where $productid is an array of 8 values (qt1-qty8). $cartSizes is stored in a $_SESSION array.

    example $cartSizes array that has 3 $productid:

    [cartSizes] => Array ( [8061ABL] => Array ( [1] => 0 [2] => 3 [3] => 4 [4] => 5 [5] => 0 [6] => 0 [7] => 0 [8] => 0 ) [6105AAOAU] => Array ( [1] => 3 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 ) [8001JNA/WH] => Array ( [1] => 0 [2] => 5 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 ) )

    i want to make a function to delete some contents ($productid) from this array when i press this link...the 'id' i pass is the same as $productid...so it's interchangeable.

    <a href="cart.php?action=delete&id='.$id.'" >Remove</a>

    in cart.php, i have

    switch ($action) {
         
            case 'delete':
                    $rows = count($cartSizes);
    		for ($i=0; $i<$rows;i++) {
    			if ($cartSizes[$i] = $_GET['id'] {
    				unset($cartSizes[$i];
    			}
    		}
    		$cartSizes = array_values($cartSizes);
    break;}
    Code (markup):
    this doesn't work....it's an associative array where $productid is a string of alphanumerics, is this what's wrong?
     
    vassili, Feb 13, 2008 IP
  2. vassili

    vassili Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    huh, i mixed up associative and regular arrays...this seems to work...any further suggestions?

    foreach ($cartSizes as $productid) 
    		{
    			 if ($productid = $_GET['id']) 
    			 {			
    				unset($cartSizes[$productid]);
    			 }
    		}
    Code (markup):
     
    vassili, Feb 13, 2008 IP