使用 PHP 将 JSON 转换为 CSV 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20667418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting JSON to CSV format using PHP
提问by user2647092
I am trying to convert a json file into csv format using a php script. The code is as follows:
我正在尝试使用 php 脚本将 json 文件转换为 csv 格式。代码如下:
if (empty($argv[1])) die("The json file name or URL is missed\n");
$jsonFilename = $argv[1];
$json = file_get_contents($jsonFilename);
$array = json_decode($json, true);
$f = fopen('output.csv', 'w');
$firstLineKeys = false;
foreach ($array as $line)
{
if (empty($firstLineKeys))
{
$firstLineKeys = array_keys($line);
fputcsv($f, $firstLineKeys);
$firstLineKeys = array_flip($firstLineKeys);
}
fputcsv($f, array_merge($firstLineKeys, $line));
}
}
This kind of works, but is only returning the outer variables of the JSON file, and am getting a "Array to string conversion" warning
这种工作,但只返回 JSON 文件的外部变量,并收到“数组到字符串转换”警告
The JSON data looks like this:
JSON 数据如下所示:
{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000100355321","value_3":"XXXX","value_4":"12667","value_5":"6"},"stream_type":"COOKIE"}
{"type":"ATTRIBUTED","conversion":{,"value_1":"000000167865321","value_3":"YYYY","value_4":"12668","value_5":"0"},"stream_type":"COOKIE"}
{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000134535321","value_3":"AAAA","value_4":"12669","value_5":"9"},"stream_type":"COOKIE"}
{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000100357651","value_3":"WWWW","value_4":"12670","value_5":"2"},"stream_type":"COOKIE"}
The output I am getting is : type,conversion,stream_type NON_ATTRIBUTED,Array,COOKIE NON_ATTRIBUTED,Array,COOKIE
我得到的输出是: type,conversion,stream_type NON_ATTRIBUTED,Array,COOKIE NON_ATTRIBUTED,Array,COOKIE
The output I am expecting is: type,conversion,value_1,value_3,value_4, value_5 ,stream_type NON_ATTRIBUTED,000000100355321, XXXX, 1267, 6, COOKIE ..
我期待的输出是: type,conversion,value_1,value_3,value_4, value_5 ,stream_type NON_ATTRIBUTED,000000100355321, XXXX, 1267, 6, COOKIE ..
ANy help appreciated as this is very new to me
感谢任何帮助,因为这对我来说很新
回答by Andrew Surzhynskyi
json_decode($json, true); converts JSON objects to associative arrays. So this
json_decode($json, true); 将 JSON 对象转换为关联数组。所以这
{
"type":"NON_ATTRIBUTED",
"conversion":{,
"value_1":"000000100355321",
"value_3":"XXXX",
"value_4":"12667",
"value_5":"6"
},
"stream_type":"COOKIE"
}
Become this:
变成这样:
array(3) {
["type"]=> string(14) "NON_ATTRIBUTED"
["conversion"]=> array(4) {
["value_1"]=> string(15) "000000100355321"
["value_3"]=> string(4) "XXXX"
["value_4"]=> string(5) "12667"
["value_5"]=> string(1) "6"
}
["stream_type"]=> string(6) "COOKIE"
}
As you see there is nested arrays. And you trying to insert all elements of array to your text file (csv is just a simple text file) with this line:
如您所见,有嵌套数组。并且您尝试使用以下行将数组的所有元素插入文本文件(csv 只是一个简单的文本文件):
fputcsv($f, array_merge($firstLineKeys, $line));
It works nice when element of array is string. But when the element is array we got the Array to string conversion. So you must to use loop or array_merge on a nested array to prevent this.
当数组的元素是字符串时它工作得很好。但是当元素是数组时,我们得到了数组到字符串的转换。所以你必须在嵌套数组上使用 loop 或 array_merge 来防止这种情况。
I can't clearly understand how your csv must look like, but I hope this fix of your code will help you. If not, write a comment below.
我无法清楚地理解你的 csv 必须是什么样子,但我希望你的代码的这个修复能帮助你。如果没有,请在下面写评论。
if (empty($argv[1])) die("The json file name or URL is missed\n");
$jsonFilename = $argv[1];
$json = file_get_contents($jsonFilename);
$array = json_decode($json, true);
$f = fopen('output.csv', 'w');
$firstLineKeys = false;
foreach ($array as $line)
{
if (empty($firstLineKeys))
{
$firstLineKeys = array_keys($line);
fputcsv($f, $firstLineKeys);
$firstLineKeys = array_flip($firstLineKeys);
}
$line_array = array($line['type']);
foreach ($line['conversion'] as $value)
{
array_push($line_array,$value);
}
array_push($line_array,$line['stream_type']);
fputcsv($f, $line_array);
}
There is also a mistake in your json - unneeded comma: "conversion":{,
您的 json 中也有一个错误 - 不需要的逗号: "conversion":{,

