title: How to create multidemensional array and keep adding to it using a session
body: <p>I am trying to create a shopping cart for my website and want to be able to add products to a basket. </p>
  
  <p>So I created an ajax post to a cart.php file in which I start a session if it doesn't exist and then add to it, or only add to the session if it does.</p>
  
  <p>This works when I don't specify the keys like so:</p>
  
  <pre><code>if(isset($_POST['product'])){
    if (isset($_SESSION['cart'])) {
      $_SESSION['cart'][] = $_POST['product'];
      $_SESSION['cart'][] = $_POST['price'];
      $_SESSION['cart'][] = $_POST['picture'];
      $_SESSION['cart'][] = $_POST['quantity'];
   } else {
      //Session is not set, setting session now
      $_SESSION['cart'] = array();
      $_SESSION['cart'][] = $_POST['product'];
      $_SESSION['cart'][] = $_POST['price'];
      $_SESSION['cart'][] = $_POST['picture'];
      $_SESSION['cart'][] = $_POST['quantity'];
   }
  }
  </code></pre>
  
  <p>This gives me an array like this:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Douche 1
      [1] =&gt; 1200
      [2] =&gt; cms/images/douche.jpg
      [3] =&gt; 
      [4] =&gt; Douche 1
      [5] =&gt; 1200
      [6] =&gt; cms/images/douche.jpg
      [7] =&gt; 
      [8] =&gt; Douche 1
      [9] =&gt; 1200
      [10] =&gt; cms/images/douche.jpg
      [11] =&gt; 
      [12] =&gt; Douche 1
      [13] =&gt; 1200
      [14] =&gt; cms/images/douche.jpg
      [15] =&gt; 
  )
  </code></pre>
  
  <p>But later on I want to loop the result, so I need the keys to have useable values not just numbers. So I tried this:</p>
  
  <pre><code>if(isset($_POST['product'])){
    if (isset($_SESSION['cart'])) {
      $_SESSION['cart']['product'] = $_POST['product'];
      $_SESSION['cart']['price'] = $_POST['price'];
      $_SESSION['cart']['picture'] = $_POST['picture'];
      $_SESSION['cart']['quantity'] = $_POST['quantity'];
   } else {
      //Session is not set, setting session now
      $_SESSION['cart'] = array();
      $_SESSION['cart']['product'] = $_POST['product'];
      $_SESSION['cart']['price'] = $_POST['price'];
      $_SESSION['cart']['picture'] = $_POST['picture'];
      $_SESSION['cart']['quantity'] = $_POST['quantity'];
   }
  }
  </code></pre>
  
  <p>Which gives me the following result:</p>
  
  <pre><code>Array
  (
      [product] =&gt; Douche 1
      [price] =&gt; 1200
      [picture] =&gt; cms/images/douche.jpg
      [quantity] =&gt; 
  )
  </code></pre>
  
  <p>It replaces itself everytime, so what I am needing is a multidimensional array. My question: how do I create it?</p>
  
  <p>This is the desired result for me:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [product] =&gt; Douche 1
              [price] =&gt; 1200
              [picture] =&gt; cms/images/douche.jpg
              [quantity] =&gt; 
          )
  
      [1] =&gt; Array
          (
              [product] =&gt; Douche 1
              [price] =&gt; 1200
              [picture] =&gt; cms/images/douche.jpg
              [quantity] =&gt;  =&gt; 18
          )
  )
  </code></pre>