从 JSON 数据生成自定义 CSV 导出
Generate custom CSV export from JSON data
在我的一个 ModelAdmin 选项卡中,有一列包含 JSON 编码的数据,我想将其转换为 CSV table 并导出。我见过的大多数问题都是直接从 table 导出数据行,但是如何在将数据转换为 CSV table 进行导出之前先从其中一列中提取数据呢?
任何想法将不胜感激。
当您使用 PHP 并希望输出为 CSV 时,您应该做的第一件事就是将 JSON 转换为数组。然后你可以使用一些 PHP 函数将结果提示为 CSV。让我们来编码:
$arr = json_decode($jsonInput, true);
if(empty($arr)) {
die("INVALID JSON");
}
$filename = "your-filename.csv";
$now = gmdate("D, d M Y H:i:s");
header("Expires: $now GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/csv");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
$df = fopen('php://output', 'w');
fputs($df, $bom = ( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
fputcsv($df, array_keys(reset($arr)), ';');
foreach($arr as $row) {
fputcsv($df, $row, ';');
}
fclose($df);
@KikoGarcia,感谢您的投入。但是,这样做会更简单。此外,当我尝试您的代码时,它会将整个页面复制到导出的 CSV 文件中。 fclose();
后必须有一个exit();
。
将 JSON 数据转换为数组后,我必须遍历并创建另一个数组,因为其中有我需要导出的嵌套数据。一旦我得到最终的数组,我就调用这个函数,即 $this->arrayToCSVExport($finalArr);
public function arrayToCSVExport($array, $filename = "backup.csv") {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'";');
$bookdata = fopen('php://output', 'w');
// output the column headings
fputcsv($bookdata, array(
'Customer Type',
'Company Name',
'Contact First Name',
'Contact Last Name',
'Contact Phone',
'Contact Mobile',
'Contact Email',
'Contact Address',
'Course Name',
'Event ID',
'Student First Name',
'Student Last Name',
'Student Phone',
'Student Mobile',
'Student Email',
'Student Address',
'Purchase Order'
));
foreach ($array as $user) {
fputcsv($bookdata, $user);
}
fclose($bookdata);
exit();
}
在我的一个 ModelAdmin 选项卡中,有一列包含 JSON 编码的数据,我想将其转换为 CSV table 并导出。我见过的大多数问题都是直接从 table 导出数据行,但是如何在将数据转换为 CSV table 进行导出之前先从其中一列中提取数据呢?
任何想法将不胜感激。
当您使用 PHP 并希望输出为 CSV 时,您应该做的第一件事就是将 JSON 转换为数组。然后你可以使用一些 PHP 函数将结果提示为 CSV。让我们来编码:
$arr = json_decode($jsonInput, true);
if(empty($arr)) {
die("INVALID JSON");
}
$filename = "your-filename.csv";
$now = gmdate("D, d M Y H:i:s");
header("Expires: $now GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/csv");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
$df = fopen('php://output', 'w');
fputs($df, $bom = ( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
fputcsv($df, array_keys(reset($arr)), ';');
foreach($arr as $row) {
fputcsv($df, $row, ';');
}
fclose($df);
@KikoGarcia,感谢您的投入。但是,这样做会更简单。此外,当我尝试您的代码时,它会将整个页面复制到导出的 CSV 文件中。 fclose();
后必须有一个exit();
。
将 JSON 数据转换为数组后,我必须遍历并创建另一个数组,因为其中有我需要导出的嵌套数据。一旦我得到最终的数组,我就调用这个函数,即 $this->arrayToCSVExport($finalArr);
public function arrayToCSVExport($array, $filename = "backup.csv") {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'";');
$bookdata = fopen('php://output', 'w');
// output the column headings
fputcsv($bookdata, array(
'Customer Type',
'Company Name',
'Contact First Name',
'Contact Last Name',
'Contact Phone',
'Contact Mobile',
'Contact Email',
'Contact Address',
'Course Name',
'Event ID',
'Student First Name',
'Student Last Name',
'Student Phone',
'Student Mobile',
'Student Email',
'Student Address',
'Purchase Order'
));
foreach ($array as $user) {
fputcsv($bookdata, $user);
}
fclose($bookdata);
exit();
}