I just needed to do the same. I wrote the small command line script, which take as a parameter the json file and output the CSV.
You can check it here: PHP Converting JSON array to CSV
The important staff there is using keys of array as the first row in CSV file.
And maintaining the order on the next elements, to not mess up the CSV.
Here is the 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('php://output', 'w');
$firstLineKeys = false;
foreach ($array as $line)
{
if (empty($firstLineKeys))
{
$firstLineKeys = array_keys($line);
fputcsv($f, $firstLineKeys);
$firstLineKeys = array_flip($firstLineKeys);
}
// Using array_merge is important to maintain the order of keys acording to the first element
fputcsv($f, array_merge($firstLineKeys, $line));
}