title: jQuery AJAX how to use the object array?
body: <p>So I have two files: index.php and query.php. </p>
  
  <p>The problem is, I don't know how to use the array (msg.d) retrieved by ajax. The output of my array is:</p>
  
  <pre><code>{"s":0,"d":[{"userPostID":"1","userID":"1","postID":"1","choice":"1"},{"userPostI‌​D":"2","userID":"1","postID":"2","choice":"0"},{"userPostID":"3","userID":"1","pos‌​tID":"3","choice":"1"}]}
  </code></pre>
  
  <p>What I want to do is to loop through the array so that </p>
  
  <pre><code>while (i &lt; array.length){ 
        if (msg.d[i]['choice'] = 1) {
             //do something with msg.d[i]['postID']
        } else if (msg.d[i]['choice'] = 0) {
             //do something else with msg.d[i]['postID']
        }
        i++
  }
  </code></pre>
  
  <p>I've never worked with object arrays before and from what I can gather, what I'm trying to do is rather complicated and I can't figure out the examples I find.</p>
  
  <p>index.php </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function() {
  // set $checked values
  
      $.ajax({
          url: 'query.php',
          type: 'POST',
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          async: true,
          cache: false,
          success: function (msg) {
                  console.log(msg);
                  },
          error: function (x, e) {
              alert("The call to the server side failed.");
          }
      });
  });
  &lt;/script&gt;
  
  &lt;?php
  $data = mysql_query("SELECT * FROM Posts LEFT JOIN userPosts ON Posts.postID = userPosts.postID AND userPosts.userID = $userID") or die(mysql_error());
  
  while($row = mysql_fetch_array( $data )){
  ?&gt;
  </code></pre>
  
  <p>query.php</p>
  
  <pre><code>&lt;?php
  
  $query = mysql_query("SELECT * FROM userPosts WHERE userPosts.userID = $userID") or die(mysql_error());
  
  if (mysql_num_rows($query) == 0)
  {
  echo 'error';
  }
  else
  {
  
  echo json_encode(mysql_fetch_assoc($query));
  }
  ?&gt;
  </code></pre>
  
  <p>I know, I'm close...
  There are no errors!</p>