title: php mysql shopping cart updating quantity of an item using 2d array
body: <p>The shopping cart I am building only seems to update the quantity for the first element of the array. So for example the first item in my shopping cart would have a quantity of 1 and then when I added another quantity of 2 from the products page the total then changes to 3 which is what I want. If I however repeat these steps for another item it will add them into the array separately rather than grouping them together</p>
  
  <pre><code>if(isset($_GET['add'])){
  foreach ($_SESSION['cart'] as $key =&gt; $item){
              if ($item['id'] == $itemID) {
  
                  $newQuan = $item['quantity'] + $quantity;
  
                  unset($_SESSION['cart'][$key]);
  
                  $_SESSION['cart'][] = array("id" =&gt; $itemID,"quantity" =&gt; $newQuan);
                  header('Location:xxx');//stops user contsanlty adding on refresh
                  exit;
              }
              else{
                  $_SESSION['cart'][] = array("id" =&gt; $itemID,"quantity" =&gt; $quantity);
                  header('xxx');//stops user contsanlty adding on refresh
                  exit;
              }
          }
      }
  </code></pre>
  
  <p>Can anyone help me out as to why the first element gets updated only ?</p>