Php 将json转换为csv将返回数组、数组
我试图将json文件转换为csv,但csv文件中只打印文本“array,array”。我猜这是因为返回了多个数组,但作为noob,我不知道如何修复这个问题Php 将json转换为csv将返回数组、数组,php,json,csv,Php,Json,Csv,我试图将json文件转换为csv,但csv文件中只打印文本“array,array”。我猜这是因为返回了多个数组,但作为noob,我不知道如何修复这个问题 <?php $jsonString = file_get_contents("data.json"); //Decode the JSON and convert it into an associative array. $jsonDecoded = json_decode($jsonString, true); $jsonD
<?php
$jsonString = file_get_contents("data.json");
//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($jsonString, true);
$jsonDecoded = $jsonDecoded;
//Give our CSV file a name.
$csvFileName = 'example.csv';
//Open file pointer.
$fp = fopen($csvFileName, 'w');
//Loop through the associative array.
foreach($jsonDecoded as $row){
//Write the row to the CSV file.
fputcsv($fp, $row);
}
//Finally, close the file pointer.
fclose($fp);
print $jsonDecoded;
echo json_last_error_msg();
?>
您只需参考
“数据”
键即可
foreach($jsonDecoded['data'] as $row){ ...
您只需参考
“数据”
键即可
foreach($jsonDecoded['data'] as $row){ ...
您可以看到
print\r($jsondecodded)的问题
$JSONDECODE=$JSONDECODE代码>是…您可以通过print\r($jsondecodded)查看问题的可能副本
$JSONDECODE=$JSONDECODE代码>是…可能重复的
$jsonString = file_get_contents("data.json");
//Decode the JSON and convert it into an associative array.
$jsonDecoded = json_decode($jsonString, true);
$list= $jsonDecoded;
file = new SplFileObject('example.csv', 'w');
foreach ($list as $fields) {
$file->fputcsv($fields);
}