我正在尝试将sql查询返回的数组转换为CSV文件,但是由于我的csv文件的第一行变得空白,而且包含列标题的行在两个块之后开始,因此我遇到了一种奇怪的代码行为。以下是我的代码,
$csv = "name,job,salary,age \n";
$export = $this->model2->emp_data();
foreach ($export as $key => $value) {
$csv.= $value['name'].','. $value['job'].','. $value['sal'].','.$value['age']."\n";
}
$filename ='excel.csv';
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=" . $filename);
header("Pragma: no-cache");
header("Expires: 0");
$csv_handler = fopen('php://output', 'w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
$ csv给我我的标题名称,$ export是sql返回的数据 我这是怎么了?谢谢
样本输入(查询返回的数据)-
[0] => Array
(
[name] => jeff1
[job] => DBA
[sal] => 6999
[age] => 44
[create] => test
[update] => test
)
[1] => Array
(
[name] => jeff2
[job] => DBA
[sal] => 6999
[age] => 44
[create] => test
[update] => test
)
....so on
输出-https://drive.google.com/open?id=1u0Q8OgerLAGlq_r610W7KU0N0hnu0gZE