title: Merge Multiple JSON Into Single OBject
body: <p>I have multiple JSON files that I would like to parse, edit, and merge into one object, which will ultimately get re-encoded and output as a single JSON.</p>
  
  <p>I currently have a working example of when I parse and edit one object, but I can't figure out how to do two and get a single result.</p>
  
  <p>Working example:</p>
  
  <pre><code>$source = 'http://www.jakedup.com/_/source1';
  
  $data = json_decode(file_get_contents($source));
  
  // THIS WILL BE THE FINAL OUTPUT OBJECT
  $output = new stdClass();
  
  // GET THE ARTICLE DATES AS AN ARRAY
  $tracks = get_object_vars($data-&gt;tracks);
  
  // LOOPS OVER ARTICLE PUBDATES TO GET THE OUTER OBJECTS
  foreach ($tracks as $key=&gt;$val) {
    // LOOPS OVER THE INNER OBJECTS IN EACH ARTICLE PUBDATE
    foreach ($data-&gt;tracks-&gt;$key as $object) {
      // ADD IT ONTO THE OUTPUT OBJECT WITH "ID" AS PARENT NAME
      $output-&gt;{$object-&gt;id} = $object;
    }
  }
  
  echo json_encode($output);
  </code></pre>
  
  <p>Here are my JSON examples. I am providing two examples, but I am looking for a way to merge an infinite number of JSON files dynamically.</p>
  
  <p>JSON Source1:</p>
  
  <pre><code>{
      "foo":"bar",
      "tracks":{
          "1349496000":[
              {
                  "id":"25328",
                  "is_promo":null,
                  "is_feature":null,
                  "listens":"1312",
                  "downloads":"777"
              },
              {
                  "id":"25327",
                  "is_promo":null,
                  "is_feature":null,
                  "listens":"1255",
                  "downloads":"578"
              },
              {
                  "id":"25329",
                  "is_promo":null,
                  "is_feature":null,
                  "listens":"341",
                  "downloads":"139"
              }
          ],
          "1349323200":[
              {
                  "id":"25310",
                  "is_promo":null,
                  "is_feature":null,
                  "listens":"10793",
                  "downloads":"4953"
              }
          ]
      }
  }
  </code></pre>
  
  <p>JSON Source 2:</p>
  
  <pre><code>{
      "foo":"bar",
      "tracks":{
          "1349323200":[
              {
                  "id":"25303",
                  "is_promo":null,
                  "is_feature":null,
                  "listens":"2347",
                  "downloads":"1499"
              }
          ],
          "1349236800":[
              {
                  "id":"25266",
                  "is_promo":null,
                  "is_feature":null,
                  "listens":"24485",
                  "downloads":"19366"
              }
          ]
      }
  }
  </code></pre>
  
  <p>I have been toying around with the idea of grabbing sources in a loop, but I am stuck when it gets to merging the data. All my different efforts have not produced the right data structure.</p>
  
  <p>Current test code:</p>
  
  <pre><code>$source = 'http://www.jakedup.com/_/source';
  $pages = 2;
  
      for ($i = 1; $i &lt;= $pages; $i++) {
          $sources[$i] = $source.$i;
          ${'source'.$i} = json_decode(file_get_contents($source.$i));
      }
  </code></pre>
  
  <p>I now have two objects -- $source1 &amp; $source2 -- but I'm not sure what to do next.</p>
  
  <p>This is the output I am expecting:</p>
  
  <pre><code>{
      "25328": {
          "id": "25328",
          "is_promo": null,
          "is_feature": null,
          "listens": "1312",
          "downloads": "777",
          "timestamp": "1349496000"
      },
      "25327": {
          "id": "25327",
          "is_promo": null,
          "is_feature": null,
          "listens": "1255",
          "downloads": "578",
          "timestamp": "1349496000"
      },
      "25329": {
          "id": "25329",
          "is_promo": null,
          "is_feature": null,
          "listens": "341",
          "downloads": "139",
          "timestamp": "1349496000"
      },
      "25310": {
          "id": "25310",
          "is_promo": null,
          "is_feature": null,
          "listens": "10793",
          "downloads": "4953",
          "timestamp": "1349323200"
      },
      "25303": {
          "id": "25303",
          "is_promo": null,
          "is_feature": null,
          "listens": "2347",
          "downloads": "1499",
          "timestamp": "1349323200"
      },
      "25266": {
          "id": "25266",
          "is_promo": null,
          "is_feature": null,
          "listens": "24485",
          "downloads": "19366",
          "timestamp": "1349236800"
      }
  }
  </code></pre>
  
  <p>Thanks in advance for your help!</p>