title: Is it possible extract and merge a single json object from multiple json files?
body: <p>Sorry for the confusing title. I am having a bit of an issue here merging some JSON files. I need to merge all the products into one array in a separate file. </p>
  
  <p>I have a directory full of same structured json files. I am using glob to select all files and decode->append-->encode json files into one large file. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;?php
  $files = glob("*.json");
  $newDataArray = [];
  foreach($files as $file){
      $thisData = file_get_contents($file);
      $thisDataArray = json_decode($thisData);
      $newDataArray[] = $thisDataArray;
  }
  $newDataJSON = json_encode($newDataArray);
  file_put_contents("merged.json",$newDataJSON);
  ?&gt;    
  </code></pre>
  
  <p>Now, the above code seems to work great but I only want to extract all the products.</p>
  
  <p>Quick example of what I need to achieve. </p>
  
  <p><strong>File1.json</strong></p>
  
  <pre><code>{
    "status": true,
    "user": {
      "username": "sally",
      "avatar": "/images/default-avatar.png",
      "products": [
        {
          "id": "35vR4hr",
          "title": "Picture 1",
          "image": null,
          "quantity": {
            "min": 1,
            "max": 1
          },
          "price": 2,
          "currency": "CAD",
          "stock_warning": 1,
          "type": "service",
          "stock": 9223372036854776000
        },
        {
          "id": "na1Id4t",
          "title": "Picture 2",
          "image": null,
          "quantity": {
            "min": 1,
            "max": 1
          },
          "price": 0.75,
          "currency": "CAD",
          "stock_warning": 3,
          "type": "service",
          "stock": 9223372036854776000
        }
      ]
    }
  }
  </code></pre>
  
  <p><strong>File2.json</strong></p>
  
  <pre><code>    {
    "status": true,
    "user": {
      "username": "Jessica",
      "avatar": "/images/default-avatar.png",
      "products": [
        {
          "id": "wjiefi94",
          "title": "Picture 3",
          "image": null,
          "quantity": {
            "min": 1,
            "max": 1
          },
          "price": 2,
          "currency": "CAD",
          "stock_warning": 1,
          "type": "service",
          "stock": 9223372036854776000
        },
        {
          "id": "n34idwi",
          "title": "Picture 4",
          "image": null,
          "quantity": {
            "min": 1,
            "max": 1
          },
          "price": 0.75,
          "currency": "CAD",
          "stock_warning": 3,
          "type": "service",
          "stock": 9223372036854776000
        }
      ]
    }
  }
  </code></pre>
  
  <p>I want the data to be merged like:</p>
  
  <p><strong>merged.json</strong></p>
  
  <pre><code>{
      "products": [
        {
          "id": "wjiefi94",
          "title": "Picture 1",
          "image": null,
          "quantity": {
            "min": 1,
            "max": 1
          },
          "price": 2,
          "currency": "CAD",
          "stock_warning": 1,
          "type": "service",
          "stock": 9223372036854776000
        },
        {
          "id": "n34idwi",
          "title": "Picture 2",
          "image": null,
          "quantity": {
            "min": 1,
            "max": 1
          },
          "price": 0.75,
          "currency": "CAD",
          "stock_warning": 3,
          "type": "service",
          "stock": 9223372036854776000
        },
              {
          "id": "n34idwi",
          "title": "Picture 3",
          "image": null,
          "quantity": {
            "min": 1,
            "max": 1
          },
          "price": 0.75,
          "currency": "CAD",
          "stock_warning": 3,
          "type": "service",
          "stock": 9223372036854776000
        },
              {
          "id": "n34idwi",
          "title": "Picture 4",
          "image": null,
          "quantity": {
            "min": 1,
            "max": 1
          },
          "price": 0.75,
          "currency": "CAD",
          "stock_warning": 3,
          "type": "service",
          "stock": 9223372036854776000
        }
      ]
    }
  </code></pre>
  
  <p>I hope this makes sense. I feel like I have hit a dead end here. Any help is greatly appreciated. </p>