0

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

help4bis
  • 15
  • 6

1 Answers1

1

I don't know if this will help with your issue, but I took a different approach at building the text file based on part of your code.

Here is what my approach (based on the first part of your code and on documentation examples) looks like (the line endings might need to be different depending on your operating system -- for this example, I used \r\n partly based on an example from the documentation and partly based on past experience):

<?php
// Get the file from services
$file = file_get_contents('https://services.swpc.noaa.gov/products/noaa-planetary-k-index.json');
$json = json_decode($file);

// The process for writing output to a file
$fp = fopen('output.txt', 'w');
$i = 0;
foreach ($json as $lineData) {
    // Skip the header line according to your requirements.
    // If you need to include the header line, simply remove the $i related
    // code lines from this example.
    if ($i > 0) {
        // implode() lets you combine array pieces into a string
        fwrite($fp, implode('|', $lineData) . "\r\n");
    }
    ++$i;
}
fclose($fp);
?>

Also, if you ever need this to be a csv output file in the future, you might be able to try this approach instead (but note that the date-time-stamps have double quotes around them):

<?php
// Get the file from services
$file = file_get_contents('https://services.swpc.noaa.gov/products/noaa-planetary-k-index.json');
$json = json_decode($file);

// The process for writing output to a file
$fp = fopen('output.csv', 'w');
$i = 0;
foreach ($json as $lineData) {
    // Skip the header line according to your requirements.
    // If you need to include the header line, simply remove the $i related
    // code lines from this example.
    if ($i > 0) {
        fputcsv($fp, $lineData, $delimiter='|');
    }
    ++$i;
}
fclose($fp);
?>
summea
  • 7,390
  • 4
  • 32
  • 48
  • 1
    Thanks SOOOO much that is just what I needed... Thanks again – help4bis Feb 19 '21 at 02:52
  • @help4bis, I'm glad to hear that it helped! No problem about that! Also, if you think this answer answered your question, please consider marking it as an accepted answer in the future if you have time! Thanks :) – summea Feb 19 '21 at 05:14