title: Passing JSON object to PHP with jQuery AJAX
body: <p>I have 3 different JSON objects I want to concatenate and send over to a PHP file with jQuery AJAX. I can retrieve the JSON object in the PHP file, but cannot figure out how to loop through the results. </p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>    //my 3 JSON objects:
  
      var json_obj_1{
          "test1_key":"test1_val",
          "test2_key":"test2_val",
          "test3_key":"test3_val"
      }
  
      var json_obj_2{
          "test4_key":"test4_val"
      }
  
      var json_obj_3{
          "json_arr":[
                {
                    "test1":"test2",
                    "test3":"test4"
                }
           ]
      }
  
     //concat these to send over to the PHP file:
     var my_json_obj = json_obj_1.concat(json_obj_2);
  
     //I found if I didn't stringify obj_3 they didn't concatenate for some reason
     my_json_obj = my_json_obj.concat(JSON.stringify(json_obj_3));
  
  
     //my_json_obj  now looks like this:
  
     {
          "test1_key":"test1_val",
          "test2_key":"test2_val",
          "test3_key":"test3_val"
     }
     {
          test4_key: test4_val
     }
     {
          "json_obj_3":[
              {"test1":"test2","test3":"test4"}
          ]
     }
  </code></pre>
  
  <p>Based on this question: <a href="https://stackoverflow.com/questions/10955017/sending-json-to-php-using-ajax">Sending JSON to PHP using ajax</a>, I don't stringify the final JSON object.</p>
  
  <p>Here's the AJAX call to the PHP file:</p>
  
  <pre><code>$.ajax({
          url: 'my_php_file.php',
          data: {my_json_data: my_json_obj},
          type: 'POST',
          async: false,
          dataType: 'json',
          cache:false,
          success:function(data, textStatus, jqXHR){
              console.log('AJAX SUCCESS');
          }, 
          complete : function(data, textStatus, jqXHR){
              console.log('AJAX COMPLETE');
          }
  });
  </code></pre>
  
  <p>This is what it looks like when retrieved in my PHP file:</p>
  
  <pre><code>echo $_POST['my_json_data'];
  //outputs:
  {
      "test1_key":"test1_val",
      "test2_key":"test2_val",
      "test3_key":"test3_val"
  }
  {
       test4_key: test4_val
  }
  {
      "json_obj_3":[
          {"test1":"test2","test3":"test4"}
      ]
  }
  </code></pre>
  
  <p>Here's where I run in to problems. I want to be able to loop through this in a foreach. From what I have read (<a href="https://stackoverflow.com/questions/4731242/php-loop-through-json-array">php: loop through json array</a>) I have to decode the JSON object. However when I do this and loop through the results I get: " PHP Warning:  Invalid argument supplied for foreach() ". </p>
  
  <p>Even if I stringify the JSON object before sending it over to the PHP file I get the same problem. I have been banging my head against a brick wall for hours with this. Any help will be massively appreciated. </p>
  
  <p>Thanks in advance. </p>
  
  <p><strong>Solved:</strong></p>
  
  <p>I was going wrong when concatenating my JSON objects following this question: <a href="https://stackoverflow.com/questions/10384845/merge-two-json-objects-in-to-one-object">Merge two json/javascript arrays in to one array</a>. Instead I just put them together like so:</p>
  
  <pre><code>my_json_obj = '['+json_obj_1+','+json_obj_2+', '+JSON.stringify(json_obj_3)+']';
  </code></pre>
  
  <p>I then use json_decode in my PHP file which works a treat. <a href="http://json.parser.online.fr/" rel="nofollow noreferrer">http://json.parser.online.fr/</a> was invaluable in debugging my dodgy JSON. A great tool for JSON beginners. </p>