my JSON data pattern is as follows:
{
"Contestants": [
{
"Province": "Ontario",
"Classification": "LSPI,,",
"ClassificationDate": "2021",
"RegistrationStatus": "Registered",
"FirstName": "Kyle",
"LastName": "Straunf",
"Gender": "M",
"AGE": null,
"DOB": "02/08/2003",
"Clubs": [
{
"Clubname": "Penguins",
"Code": "MPNO",
"Clubid": "200"
}
],
"Email": null,
"Language": "E",
"ChallengeData": null
},
{
"Province": "Alberta",
"Classification": "LSPI,,",
"ClassificationDate": "2021",
"RegistrationStatus": "Registered",
"FirstName": "Alexander",
"LastName": "Kentwood",
"Gender": "M",
"AGE": null,
"DOB": "08/16/2005",
"Clubs": [
{
"Clubname": "Elegant Dolphins",
"Code": "ZGIA",
"Clubid": "300"
}
],
"Email": null,
"Language": "E",
"ChallengeData": null
}
]
}
I managed to get the data like below, it prints but I'm having trouble printing each csv lines headers and data without it being overwritten. What's a good way to output it into a csv file with the keys as columns headers.
$all_data = json_decode($json_stream, true);
foreach ($all_data as $record) {
foreach ($record as $rec) {
foreach ($rec as $key => $value) {
if (is_array($value)) {
foreach ($value as $value2) {
foreach ($value2 as $key2 => $value2) {
echo "<p> key: $key2 . value: $value2</p>";
}
}
} else {
echo "<p> key: $key . value: $value</p>";
}
}
}
}
As for the expected output, I'd like all the keys to be column headers like the picture below but continued with the other headers and so on.
Classification section:
At the top is how it looks like presently however below is how it would be better

