如何通过添加标题将我的2d数组转换为csv文件,以便用户可以直接上传文件。
这是我的数组
Array (
[0] => Array
(
[0] => 25/2/2013
[1] => 8.45 a.m
[2] => 9.98
[3] => 1.23
[4] => 6.1
)
[1] => Array
(
[0] => 25/2/2013
[1] => 8.46 a.m
[2] => 9.02
[3] => 1.75
[4] => 1.75
)
)
我希望我的输出为
日期时间值1值2值3(所有标题)
25/2/2013 8.45 a.m 9.98 1.23 6.1
25/2/2013 8.46 a.m 9.02 1.75 1.75
答案 0 :(得分:10)
$header = array("Date","Time","Value1","Value2","Value3");
$fp = fopen("php://output", "w");
fputcsv ($fp, $header, "\t");
foreach($array as $row){
fputcsv($fp, $row, "\t");
}
fclose($fp);
如果要为客户端触发下载,请在顶部添加以下行。
header("Content-Type: text/csv");
header('Content-disposition: attachment;filename=mycoolfile.csv');