I have a json file I read from an external website, I then want to turn that into a txt file where each record is on a new line and the fields are delimited by "|"
I got stuck in writing it out to file.
// Get the file from services
$file = file_get_contents('https://services.swpc.noaa.gov/products/noaa-planetary-k-index.json');
// from https://stackoverflow.com/questions/3684463/php-foreach-with-nested-array so I can see the array structure
// Output array
displayArrayRecursively($json);
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $value) {
if (is_array($value)) {
//
displayArrayRecursively($value, $indent . '|');
} else {
// Output
echo "$indent $value \n";
}
}
}
}
This returns the Json file structure (which I put in there to see if things read), and the JSON file with the values delimited by "|".
I got to convert this (shortened).(see full file here : https://services.swpc.noaa.gov/products/noaa-planetary-k-index.json)
[["time_tag","Kp","Kp_fraction","a_running","station_count"],["2021-02-10 00:00:00.000","1","0.67","3","8"],["2021-02-10 03:00:00.000","0","0.33","2","8"],["2021-02-10 06:00:00.000","1","0.67","3","8"]]
To show as:
| time_tag | Kp | Kp_fraction | a_running | station_count | 2021-02-10 00:00:00.000 | 1 | 0.67 | 3 | 8 | 2021-02-10 03:00:00.000 | 0 | 0.33 | 2 | 8 | 2021-02-10 06:00:00.000 | .....
What I want is, write to txt file: 2021-02-10 00:00:00.000|1|0.67|3|8 2021-02-10 03:00:00.000|0|0.33|2|8 etc for all the records
So how do I do that...
Thanks