Convert JSON to CSV using PHP Example

Today, We want to share with you Convert JSON to CSV using PHP Example.In this post we will show you Converting JSON to CSV format using PHP, hear for converting between CSV, JSON and SQL files in PHP we will give you demo and example for implement.In this post, we will learn about converting JSONtoCSV using PHP (JSON Keys as Column Headers) with an example.

Convert JSON to CSV using PHP Example

There are the Following The simple About Properly converting JSON with arrays to CSV Full Information With Example and source code.

As I will cover this Post with live Working example to develop multidimensional jsontocsv php, so the converting JSONtoCSV is used for this example is following below.

Also Read This πŸ‘‰   Export Json Array to CSV using php Example

How to Convert JSON toCSV in PHP?

viruses.json

[
{
"Id": "1",
"Name": "Marburg virus",
"Position": "Germany",
"Year": "1967"
},
{
"Id": "2",
"Name": "Ebola virus",
"Position": "Democratic Republic of Congo",
"Year": "1976"
},
{
"Id": "3",
"Name": "Rabies",
"Position": "Africa",
"Year": "1920"
}
]

PHP Function to Convert JSONto CSV:


<!--?php function jsonToCSV($jfilename, $cfilename) { if (($json = file_get_contents($jfilename)) == false) die('Error reading json file...'); $viruses = json_decode($json, true); $fp = fopen($cfilename, 'w'); $header = false; foreach ($viruses as $row) { if (empty($header)) { $header = array_keys($row); fputcsv($fp, $header); $header = array_flip($header); } fputcsv($fp, array_merge($header, $row)); } fclose($fp); return; } ?-->

Function Usage:

convert json tocsv file

$json_filename = 'viruses.json';
$viruse_data_export_file_name = 'viruses.csv';
jsonToCSV($json_filename, $viruse_data_export_file_name);
echo 'Good Luck Successfully converted jsontocsv file. <a href="' . $viruse_data_export_file_name . '" target="_blank" rel="noopener noreferrer">Click here to open it.</a>';

complete script

<!--?php <br ?--> function jsonToCSV($jfilename, $cfilename)
{
if (($json = file_get_contents($jfilename)) == false)
die('Error reading json file...');
$viruses = json_decode($json, true);
$fp = fopen($cfilename, 'w');
$header = false;
foreach ($viruses as $row)
{
if (empty($header))
{
$header = array_keys($row);
fputcsv($fp, $header);
$header = array_flip($header);
}
fputcsv($fp, array_merge($header, $row));
}
fclose($fp);
return;
}

$json_filename = 'viruses.json';
$viruse_data_export_file_name = 'viruses.csv';

jsonToCSV($json_filename, $viruse_data_export_file_name);
echo 'Good Luck Successfully converted json tocsv file. <a href="' . $viruse_data_export_file_name . '" target="_blank" rel="noopener noreferrer">Click here to open it.</a>';
?>

Web Programming Tutorials Example with Demo

Read :

Also Read This πŸ‘‰   PHP Laravel Sessions Example Tutorial

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Convert between CSV, JSON and SQL files in PHP.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.