If you read the JSON data into an array, then iterate the array, taking the keys and saving them as headers, you should be able to then output all of that into a csv string, which can then be wrote to a file:
<?php
$string = // Read JSON into variable;
$data = json_decode($string, true);
$headers = [];
$output = [];
foreach ($data as $line => $item) {
$output[$line] = [];
foreach ($item as $key => $row) {
if (!isset($headers[$key])) {
$headers[$key] = $key;
}
$output[$line][$key] = $row;
}
}
$outputString = '';
foreach ($headers as $header) {
$outputString .= $header . ',';
}
rtrim($outputString, ',');
$outputString .= "\r\n";
foreach ($output as $row) {
foreach ($headers as $header) {
$outputString .= $row[$header] . ',';
}
rtrim($outputString, ',');
$outputString .= "\r\n";
}
// Output $outputString to file;
Could definitely be refactored a bit, but that should give you the basic logic of what you’re trying to do
CLICK HERE to find out more related problems solutions.