Home > Article > Backend Development > How to convert php json to csv
php json to csv method: First create a PHP sample file; then convert JSON to CSV through the "foreach ($json_obj as $fields) {fputcsv($fp, $fields);}" method. Can.
The operating environment of this article: windows7 system, PHP8 version, DELL G3 computer
Specific questions:
How to convert php json to csv? PHP library converts JSON to CSV?
I have a json service and need to create a script to export data to a csv file. Can anyone suggest a method or library to migrate json to csv format?
Below is an example format, but I expect the solution will have to be re-adapted to use it:
{"service_name": { key : value, key : value....} }
or:
{"service_name": [ { key : value, key : value....}, ... ] }
Solution:
I generally agree with the commenters, but if your data is prepared this way, isn't that all the pseudocode you need?
$json_str = "{'aintlist':[4,3,2,1], 'astringlist':['str1','str2']}"; $json_obj = json_decode ($json_str); $fp = fopen('file.csv', 'w'); foreach ($json_obj as $fields) { fputcsv($fp, $fields); } fclose($fp);
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert php json to csv. For more information, please follow other related articles on the PHP Chinese website!