[英]PHP Export Array to CSV
I currently have a query that returns the array below.. When I try to export the data to a CSV file I see the data twice (The columns are repeted, not the rows).. Can someone explain to my why? 我目前有一个查询,该查询返回下面的数组。当我尝试将数据导出到CSV文件时,我看到了两次数据(重复列,而不是行)。有人可以向我解释为什么吗? (I want to have each records show only once)
(我希望每个记录仅显示一次)
Array
(
[0] => Array
(
[0] => 6991
[id] => 6991
[1] => 8588
[work_num] => 8588
[2] => Test123
[name] => Test123
[3] => 1407154
[deployed] => 1407154
[4] => 2015-10-13
[created_date] => 2015-10-13
)
)
This is the code that I am using to export the data 这是我用来导出数据的代码
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('id','Work Number','Name','Deployed','Date'));
$get_data = $CLASSNAME->FuntionName(Parameters);
foreach($get_data as $data_row) {
fputcsv($output, $data_row);
}
fclose($output);
If you are using MySQL, then replace mysql_fetch_array
to mysql_fetch_row
. 如果使用MySQL,则将
mysql_fetch_array
替换为mysql_fetch_row
。
mysql_fetch_array
returns result set as both numeric and associative array. mysql_fetch_array
返回结果集,既是数字数组又是关联数组。 mysql_fetch_row
will return result set as only numeric array. mysql_fetch_row
将仅将数值集返回结果集。
Either fix your incoming data (when using PDO, use PDO::FETCH_ASSOC or PDO::FETCH_NUM instead of PDO::FETCH_BOTH, which is default) or name your columns when putting to csv: 修复您的传入数据(使用PDO时,使用PDO :: FETCH_ASSOC或PDO :: FETCH_NUM而不是PDO :: FETCH_BOTH,这是默认设置),或者在放入csv时命名您的列:
foreach($get_data as $data_row) {
fputcsv($output, array(
$data_row['id'],
$data_row['work_num'],
$data_row['name'],
$data_row['deployed'],
$data_row['created_date']
));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.