Home > Backend Development > PHP Problem > How to convert php json to csv

How to convert php json to csv

藏色散人
Release: 2023-03-10 20:18:01
Original
2487 people have browsed it

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.

How to convert php json to csv

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....}
}
Copy after login

or:

{"service_name":
        [
               { key : value, key : value....},
               ...
         ]
}
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template