title: How to save nested json output in csv file in php?
body: <p>I have generated json output using API and now I have to save this output in csv file in below structure- The column names are litle bit different from the actual name. Some records are coming as null.</p>
  <p>Example:</p>
  <p>json_data =</p>
  <pre><code>&quot;data&quot;: [
          {
              &quot;id&quot;: 1000001,
              &quot;version_modified_date__v&quot;: &quot;2022-03-01T08:23:04.000Z&quot;,
              &quot;doc_roles__sysr&quot;: {
                  &quot;responseDetails&quot;: {
                      &quot;pagesize&quot;: 250,
                      &quot;pageoffset&quot;: 0,
                      &quot;size&quot;: 22,
                      &quot;total&quot;: 22
                  },
                  &quot;data&quot;: [
                      {
                          &quot;role_name__sys&quot;: &quot;emea_project_owner__c&quot;,
                          &quot;user__sys&quot;: 3566601,
                          &quot;group__sys&quot;: null
                      },
                      {
                          &quot;role_name__sys&quot;: &quot;emea_submitter__c&quot;,
                          &quot;user__sys&quot;: 3566601,
                          &quot;group__sys&quot;: null
                      }
                  ]
              }
          },
          {
              &quot;id&quot;: 1000002,
              &quot;version_modified_date__v&quot;: &quot;2021-02-24T17:58:05.000Z&quot;,
              &quot;doc_roles__sysr&quot;: {
                  &quot;responseDetails&quot;: {
                      &quot;pagesize&quot;: 250,
                      &quot;pageoffset&quot;: 0,
                      &quot;size&quot;: 9,
                      &quot;total&quot;: 9
                  },
                  &quot;data&quot;: [
                      {
                          &quot;role_name__sys&quot;: &quot;owner__c&quot;,
                          &quot;user__sys&quot;: 8272503,
                          &quot;group__sys&quot;: null
                      },
                      {
                          &quot;role_name__sys&quot;: &quot;reference_specialist__c&quot;,
                          &quot;user__sys&quot;: null,
                          &quot;group__sys&quot;: 1461227836262
                      }
                  ]
              }
          }
      ]
  </code></pre>
  <p>I want in CSV file in below format -</p>
  <pre><code>id       version_modified_date     role name               user id     group
  1000001  2022-03-01T08:23:04.000Z  emea_project_owner__c   3566601
  1000001  2022-03-01T08:23:04.000Z  emea_submitter__c       3566601
  1000002  2021-02-24T17:58:05.000Z  owner__c                8272503
  1000002  2021-02-24T17:58:05.000Z  reference_specialist__c             1461227836262
  </code></pre>
  <p>I tried below code but didn't get any luck. Please help me to correct this code-</p>
  <pre><code>                        $content = json_decode(json_encode($content), true);
                          $fp = fopen('doc_roles__sys.csv', 'w');
                          foreach ($content['data'] as $item) {
                              $output['id'] = $item['id'];
                              $output['version_modified_date'] = $item['version_modified_date__v'];
                              
                              $output['role name'] = $item['data'][0]['role_name__sys'];
                              $output['user id'] = $item['data'][0]['user__sys'];
                              $output['group'] = $item['data'][0]['group__sys'];
                              //$output['document_id'] = $item['data'][0]['document_id'];
                              foreach ($output as $line) {
                                   fputcsv($fp, $item);
                              }
                          }   
  
                          echo $output;
  
                          //$fp = fopen('doc_roles__sys.csv', 'a');
                          //foreach ($output as $line) {
                          //  fputcsv($fp, $item);
                          //}
                          fclose($fp);
  </code></pre>
  <p>The output I am getting from above code is -</p>
  <pre><code>1077079,Array
  1077079,Array
  1077079,Array
  1077079,Array
  1077079,Array
  </code></pre>