title: How to convert Json to CSV in php
body: <p>Hi I am trying to convert a json file with differrent array structure than the normal one into csv file. I had been trying to find out a solution to convert it into a csv file but I cant find the solution.</p>
  
  <pre><code>  &lt;?php 
      $jsondata = '[{
          "accession_number_original": "2012.11.45",
          "author_birth_date": [
            "1932"
          ],
          "author_date": [
            "1932"
          ],
          "author_death_date": [
            ""
          ],
          "author_description": [
            "American"
          ],
          "author_display": [
            "Day yon"
          ],
          "author_names_first": [
            "Day"
          ],
          "author_names_last": [
            "yon"
          ],
          "author_names_middle": [
            ""
          ],
          "image_height": "12 1/2",
          "image_width": "18 1/4",
          "jp2_image_url": "",
          "location_physical_location": "Art Gallery",
          "location_shelf_locator": "Unknown",
          "master_image_url": "",
          "note_provenance": "Gift of Gary Ginsberg and Susanna Aaron",
          "object_date": "1963/2010",
          "object_depth": "",
          "object_height": "",
          "object_width": "",
          "origin_datecreated_end": "1963",
          "origin_datecreated_start": "1963",
          "physical_description_extent": [
            "12 1/2 x 18 1/4"
          ],
          "physical_description_material": [
            "Gelatin silver print"
          ],
          "physical_description_technique": [
            "Gelatin silver print"
          ],
          "pid": "bdr:123456",
          "title": "As Demonstrators"
        }]';
  
      $jsonDecoded = json_decode($jsondata);
      print_r('&lt;pre&gt;');
      print_r($jsonDecoded);
      print_r('&lt;/pre&gt;');
      $fh = fopen('fileout.csv', 'w');
      if (is_array($jsonDecoded)){
        print_r('&lt;-------- line variable output--------&gt;');   
        foreach($jsonDecoded as $line){
              print_r('&lt;pre&gt;'); print_r($line); print_r('&lt;/pre&gt;');
          print_r('&lt;-------- data variable output--------&gt;');
          if (is_array($line)||is_object($line)){
            foreach($line as $data){
              fputcsv($fp,$data);
              }
            }
          }
        }
      }
  
     fclose($fh);
     print_r('Converted Successfully');
  ?&gt;
  </code></pre>
  
  <p>I tried looking into most of the similar questions in the stackoverflow but none has a array of my kind so they are not much useful to me.</p>
  
  <p>If I use single foreach I am getting error <strong>Array to String Conversion failed</strong> and <strong>Array</strong> is being printed as value instead of actual data into the csv file.</p>
  
  <p>If I use two foreach I am getting error <strong>fputcsv() expects parameter 2 to be array string given</strong> </p>
  
  <p>var_dump or print_r result of decoded json is as follows</p>
  
  <pre><code>Array
  (
  [0] =&gt; stdClass Object
      (
          [accession_number_original] =&gt; 2012.11.45
          [author_birth_date] =&gt; Array
              (
                  [0] =&gt; 1932
              )
  
          [author_date] =&gt; Array
              (
                  [0] =&gt; 1932
              )
  
          [author_death_date] =&gt; Array
              (
                  [0] =&gt; 
              )
  
          [author_description] =&gt; Array
              (
                  [0] =&gt; American
              )
  
          [author_display] =&gt; Array
              (
                  [0] =&gt; Day yon
              )
  
          [author_names_first] =&gt; Array
              (
                  [0] =&gt; Day
              )
  
          [author_names_last] =&gt; Array
              (
                  [0] =&gt; yon
              )
  
          [author_names_middle] =&gt; Array
              (
                  [0] =&gt; 
              )
  
          [image_height] =&gt; 12 1/2
          [image_width] =&gt; 18 1/4
          [jp2_image_url] =&gt; 
          [location_physical_location] =&gt; Art Gallery
          [location_shelf_locator] =&gt; Unknown
          [master_image_url] =&gt; 
          [note_provenance] =&gt; Gift of Gary Ginsberg and Susanna Aaron
          [object_date] =&gt; 1963/2010
          [object_depth] =&gt; 
          [object_height] =&gt; 
          [object_width] =&gt; 
          [origin_datecreated_end] =&gt; 1963
          [origin_datecreated_start] =&gt; 1963
          [physical_description_extent] =&gt; Array
              (
                  [0] =&gt; 12 1/2 x 18 1/4
              )
  
          [physical_description_material] =&gt; Array
              (
                  [0] =&gt; Gelatin silver print
              )
  
          [physical_description_technique] =&gt; Array
              (
                  [0] =&gt; Gelatin silver print
              )
  
          [pid] =&gt; bdr:123456
          [title] =&gt; As Demonstrators
      )
  )
  </code></pre>