title: Passing associative array through AJAX to PHP
body: <p>I'm trying to pass this to a PHP script through AJAX:</p>
  
  <pre><code>  var answers={};
    for (x=0; x&lt; allAnswers.length; x++)
     {
         answers.x=new Array();
         answers.x['id']==allAnswers[x]['id'];
         answers.x['val']=$("#field_" + x).val();
     }
  
     var data={};
     data.id=questions[qId]['id'];
     data['answers']=answers;
  
     $.post('index.php',data);
  </code></pre>
  
  <p>The PHP is set to <code>print_r($_POST)</code>, and this is the output:</p>
  
  <pre><code>answers [object Object]
  
  id       3
  </code></pre>
  
  <p>What am I done wrong?</p>
  
  <p>Edit: Changing the code to use arrays, i.e:</p>
  
  <pre><code>  var answers=new Array();
     for (x=0; x&lt; allAnswers.length; x++)
     {
         answers[x]=new Array();
         answers[x]['id']=allAnswers[x]['id'];
         answers[x]['val']=$("#field_" + x).val();
     }
     var data={};
     data.id=questions[qId]['id'];
     data['answers[]']=answers;
  
     $.post('index.php',data);
  </code></pre>
  
  <p>Gives this print_r:</p>
  
  <pre><code>Array
  (
      [id] =&gt; 3
      [answers] =&gt; Array
          (
              [0] =&gt; 
              [1] =&gt; 
          )
  
  )
  </code></pre>
  
  <p>Thoughts?</p>