title: Converting JSON to CSV format using PHP
body: <p>I am trying to convert a json file into csv format using a php script.  The code is as follows:</p>
  
  <pre><code>if (empty($argv[1])) die("The json file name or URL is missed\n");
  $jsonFilename = $argv[1];
  
  $json = file_get_contents($jsonFilename);
  $array = json_decode($json, true);
  $f = fopen('output.csv', 'w');
  
  $firstLineKeys = false;
  foreach ($array as $line)
  {
      if (empty($firstLineKeys))
      {
              $firstLineKeys = array_keys($line);
              fputcsv($f, $firstLineKeys);
              $firstLineKeys = array_flip($firstLineKeys);
      }
  
  fputcsv($f, array_merge($firstLineKeys, $line));
  </code></pre>
  
  <p>}</p>
  
  <p>This kind of works, but is only returning the outer variables of the JSON file, and am getting a "Array to string conversion" warning</p>
  
  <p>The JSON data looks like this:</p>
  
  <pre><code>{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000100355321","value_3":"XXXX","value_4":"12667","value_5":"6"},"stream_type":"COOKIE"}
  {"type":"ATTRIBUTED","conversion":{,"value_1":"000000167865321","value_3":"YYYY","value_4":"12668","value_5":"0"},"stream_type":"COOKIE"}
  {"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000134535321","value_3":"AAAA","value_4":"12669","value_5":"9"},"stream_type":"COOKIE"}
  {"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000100357651","value_3":"WWWW","value_4":"12670","value_5":"2"},"stream_type":"COOKIE"}
  </code></pre>
  
  <p>The output I am getting is :
  type,conversion,stream_type
  NON_ATTRIBUTED,Array,COOKIE
  NON_ATTRIBUTED,Array,COOKIE</p>
  
  <p>The output I am expecting is:
  type,conversion,value_1,value_3,value_4, value_5 ,stream_type
  NON_ATTRIBUTED,000000100355321, XXXX, 1267, 6, COOKIE
  ..</p>
  
  <p>ANy help appreciated as this is very new to me </p>