我正在尝试使用以下代码将某些数据导出为csv,但它会附加一个不需要的' null'在档案结束。
$array = array();
$fileName = 'test.csv';
$array[0] = ['Name','Email'];
$array[1] = ['Test','test@email.com'];
$fileObj = new FileStream();
$fileObj->array_to_csv_download($array,$fileName);
array_to_csv_download代码 -
$f = fopen('php://memory', 'w');
foreach ($array as $line) {
$this -> logObj -> LogError(" CSV ".json_encode($line));
fputcsv($f, $line, $delimiter);
}
fseek($f, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'";');
fpassthru($f);
输出 -
Name,Email
Test,test@email.com
null
我错过了什么吗?
答案 0 :(得分:1)
如何直接生成CSV?适合我。
header('Content-Type: application/csv');
foreach($array as $fields) {
echo implode(",", $fields)."\n";
}