title: Iterate over KEYLESS JSON array?
body: <p>I'm building a shopping cart and using a cookie to store the cart contents in a JSON object.</p>
  
  <p>On each page, using Javascript, I need to retrieve the cookie, parse the JSON and iterate over each row, to display the described product in the basket summary.</p>
  
  <p>I decided not to use keys in the array, since if I have a record of the designation of each position, why do I need to store it in the cookie. I thought that was a smart idea, but now all my searching turns up are suggestions to use the keys.</p>
  
  <p>This is a sample of the JSON:</p>
  
  <pre><code>{
     "9999.9999":["CentOS6",2,"0.5",150,"23.90"],
     "0002.0004":["Support5","","3",5,"12.99"],
     "9999.9999":["UbuntuServer12.04LTS",1,"1",220,"42.60"]
  }
  </code></pre>
  
  <p>I was expecting to be able to access the 'price' for example (which is in the last field of each row) of the 2nd record, like so:</p>
  
  <pre><code>basket = JSON.parse(basket);
  price = basket[1][4];
  </code></pre>
  
  <p>That doesn't seem to work.</p>
  
  <p>There are really two parts to this question.</p>
  
  <ol>
  <li>How to cherry pick a value directly from the basket?</li>
  <li>How to iterate through it, allowing me to operate on each row in turn?</li>
  </ol>
  
  <p>In case you're wondering about the first field. That is the SKU of the product. The reason there are two "9999.9999" is because that is a reserved number, indicating that the product was a 'custom' product (doesn't refer directly to a catalogue item).</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Based on @Samer's answer below, I have updated the code to use arrays, as follows:</p>
  
  <p>The JSON now looks like this:</p>
  
  <pre><code>[
     ["9999.9999","CentOS6",2,"0.5",150,"23.90"],
     ["0002.0004","Support5","","3",5,"12.99"],
     ["9999.9999","UbuntuServer12.04LTS",1,"1",220,"42.60"]
  ]
  </code></pre>
  
  <p>I'm now attempting to access it from the cookie as follows:</p>
  
  <pre><code>var basket = readCookie('basket');
  basket     = JSON.parse(basket);
  alert( 'Price of item 2 =' + basket[1][5] );
  </code></pre>
  
  <p>Only, instead of alerting, 'Price of item 2 = 12.99' I get 'Price of item 2 = undefined'.</p>
  
  <p>I'm wondering what I've done wrong?</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Still having trouble with this. Maybe I should explain how we're getting the data into the cookie.</p>
  
  <p>The array on the cookie can be updated on the server using PHP (when adding custom items) or client-side using Javascript when a catalogue item is added.</p>
  
  <p>Currently, I'm concentrating on items added server side. I'm using the following code to add the cookie:</p>
  
  <pre><code>&lt;?
  extract($_POST, EXTR_SKIP);
  
  // Get contents of existing basket (if any) minus last ']'
  // so that it's ready to be added to.
  
  if (isset($_COOKIE['basket'])) {
  
      $basket = json_decode($_COOKIE['basket']);
      $chars = strlen($basket)-1;
      //echo "&lt;pre&gt;".var_dump($basket)."&lt;/pre&gt;";
  
      $cookieVal = substr($basket, 0, $chars);
  
      $cookieVal .= ",
      [\"9999.9999\",\"$sltOS\",$vcpuPoints,\"".$ramPoints."\",$storagePoints,\"".$baseServerCostMonthUSD."\"]]";
  
      $cookieVal = json_encode($cookieVal);
      /*
       * Sample format
       * 
      [["9999.9999","CentOS6",2,"0.5",150,"23.90"],
      ["0002.0004","Support5","","3",5,"12.99"],
      ["9999.9999","UbuntuServer12.04LTS",1,"1",220,"42.60"]]
       * 
       */
  
  } else {
  
      $cookieVal = json_encode("[[\"9999.9999\",\"$sltOS\",$vcpuPoints,\"".$ramPoints."\",$storagePoints,\"".$baseServerCostMonthUSD."\"]]");
  
  }
  
  $exp = time()+3600; // sets expiry to 1 hour;
  setcookie ( 'basket' , $cookieVal, $exp, '/' );
  </code></pre>
  
  <p>Then client-side for reading the cookie, I have this, which is called when the page is loaded:</p>
  
  <pre><code>function initBasket() {
  
      var basket = readCookie('basket');
  
      if (isJSON(basket)) {
  
          // We'll populate the basket
  
          basket = JSON.parse(basket);
          alert(basket[1][5]);
  
      } else {
  
          $('#sList').html('Your basket is empty.');
          return;
  
      }
  
  }
  </code></pre>
  
  <p><strong>UPDATE 3</strong>
  Finally got it working. Thanks for your help.</p>
  
  <p>Maybe the code can help others, so I've included the final code below:</p>
  
  <p>The PHP:</p>
  
  <pre><code>if (isset($_COOKIE['basket'])) {
  
      $cookieVal      = json_decode($_COOKIE['basket']);
      $cookieVal[]    = array
                          (
                              "9999.9999",
                              $sltOS,
                              $vcpuPoints,
                              $ramPoints,
                              $storagePoints,
                              $baseServerCostMonthUSD
                          );
      $cookieVal      = json_encode($cookieVal);
  
  } else {
  
      $cookieArr[]    = array
                          (
                              "9999.9999",
                              $sltOS,
                              $vcpuPoints,
                              $ramPoints,
                              $storagePoints,
                              $baseServerCostMonthUSD
                          );
      $cookieVal      = json_encode($cookieArr);
  
  }
  
  // Save VPS choice in basket cookie
  $exp = time()+3600; // sets expiry to 1 hour;
  setcookie ( 'basket' , $cookieVal, $exp, '/' );
  </code></pre>
  
  <p>The Javascript:</p>
  
  <pre><code>function initBasket() {
  
      var basket = readCookie('basket');
  
      if (isJSON(basket)) {
  
          // We'll populate the basket
          basket = JSON.parse(basket);
          alert(basket[1][5]);
  
      } else {
  
          $('#sList').html('Your basket is empty.');
          return;
  
      }
  
  }
  </code></pre>