title: passing structured data to php via post
body: <p>Suppose I have the following data:</p>
  
  <pre><code>var arr = [], arr1 = [], arr2 = [], arr3 = [], arr4 = [];
  var a = 'something', b = 'else';
  arr1['key1-1'] = 'value1-2';
  arr1['key1-2'] = 'value1-2';
  for (var i = 0; i &lt; someCond; i++) {
      arr = [];
      arr['key2-1'] = 'value2-1';
      arr['key2-2'] = 'value2-2';
      arr2.push(arr);
  }
  </code></pre>
  
  <p>Now I need to pass the hole of it to a php script.</p>
  
  <p>I packaged it into a single variable like so:</p>
  
  <pre><code>var postVar = {
      a: a,
      b: b,
      arr1: arr1,
      arr2: arr2
  };
  </code></pre>
  
  <p>I'm using jQuery so I tried to post it like this:<br/>
  <strong>1)</strong></p>
  
  <pre><code>//Works fine for a and b, not for the arrays
  $.post('ajax.php', postVar, function(response){});
  </code></pre>
  
  <p>and this:<br/>
  <strong>2)</strong></p>
  
  <pre><code>var postVar = JSON.stringify(postVar);
  $.post('ajax.php', {json: postVar}, function(response){});
  </code></pre>
  
  <p>with the php file</p>
  
  <pre><code>$req = json_decode(stripslashes($_POST['json']), true);
  </code></pre>
  
  <p>which also doesn't work.</p>
  
  <p>How should I structure/format my data as to send it to PHP?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT:</strong><br/>
  Case 1:
  console.log(postVar);
  <img src="https://i.stack.imgur.com/lR6Hz.png" alt="console.log(postVar)">
  <br/>
  <br/>
  PHP print_r($_POST) response:
  Array
  (
      [a] => something
      [b] => else
  )</p>
  
  <p>As you can see, there are no arrays (objects) on the php side.</p>
  
  <p><strong>Case 2:</strong><br/>
  When I add the following:</p>
  
  <pre>
  
      postVar = JSON.stringify(postVar);
      console.log(postVar);
  
  </pre>
  
  <p>I get<br/>
  {"a":"something","b":"else","arr1":[],"arr2":[[],[],[]]} <br/>
  with console.log(postVar)</p>
  
  <p>
  So that seems to be the problem in this case... right?
  </p>