title: How to merge 2 JSON files into one JSON FeatureCollection
body: <p>I have two JSON files each containing a FeatureCollection with the same structure but different data. I'm trying to combine them into a single JSON file as a single FeatureCollection containing all the data. I've managed to almost do this but "FeatureCollection" is being repeated at the start of the file making it invalid JSON.</p>
  
  <p>I think it is something to do with the way I'm JSON encoding the two files individually and then again when I'm combining them but after a day of trying various combinations I haven't managed to figure it out.</p>
  
  <p>This is the code which creates the first JSON file (where $results is generated by a database query):</p>
  
  <pre><code>$geojson = array( 'type' =&gt; 'FeatureCollection', 'features' =&gt; array());
  
  while ( $results-&gt;fetch() ) {
      $feature = array(
          'type' =&gt; 'Feature', 
                'properties' =&gt; array(
              'name' =&gt; $results-&gt;field('name')
              ),
        'geometry' =&gt; array(
          'type' =&gt; 'Point',
          'coordinates' =&gt; array((float)$results-&gt;field('long'), (float)$results-&gt;field('lat'))
              )
          );
      array_push($geojson['features'], $feature);
  };
  
  // // Create JSON file
      $fp = fopen('file1.json', 'w');
  fwrite($fp, json_encode($geojson));
  fclose($fp);
  </code></pre>
  
  <p>The second file (file2.json) is created in the same way, eg:</p>
  
  <pre><code>$geojson = array( 'type' =&gt; 'FeatureCollection', 'features' =&gt; array());
  
  while ( $results-&gt;fetch() ) {
      $feature = array(
          'type' =&gt; 'Feature', 
                'properties' =&gt; array(
              'name' =&gt; $results-&gt;field('name')
              ),
        'geometry' =&gt; array(
          'type' =&gt; 'Point',
          'coordinates' =&gt; array((float)$results-&gt;field('long'), (float)$results-&gt;field('lat'))
              )
          );
      array_push($geojson['features'], $feature);
  };
  
  // // Create JSON file
      $fp = fopen('file2.json', 'w');
  fwrite($fp, json_encode($geojson));
  fclose($fp);
  </code></pre>
  
  <p>I'm then combining them using this code:</p>
  
  <pre><code>    $jsonString = file_get_contents('file2.json');
      $jsonString2 = file_get_contents('file1.json');   
      $data = json_decode($jsonString, true);
      $data2 = json_decode($jsonString2, true);
  
      $op = array_merge_recursive( $data, $data2 );
  
      $fp = fopen('file3.json', 'w');
      fwrite($fp, json_encode($op));
      fclose($fp); 
  </code></pre>
  
  <p>The resulting file is mainly fine, it contains all the data that I require and is properly formatted apart from the fact that at the start of the file it has:</p>
  
  <pre><code>{"type":["FeatureCollection","FeatureCollection"],"features":[{"type":"Feature","properties":{"name":"......etc
  </code></pre>
  
  <p>Instead of:</p>
  
  <pre><code>{"type":["FeatureCollection"],"features":[{"type":"Feature","properties":{"name":"......etc
  </code></pre>
  
  <p>I can't figure out why there are two instances of "FeatureCollection" at the beginning or how to only produce one.</p>