使用 fputcsv 将数组放入 CSV 中


Putting an array in to a CSV using fputcsv

我的表中有很多字段,我已经序列化了这些字段以减少字段的数量。对于其他一切完美运行的东西,因为它存储数据,在需要时,我可以使用以下作为示例:

$dateofbirth = unserialize($row['dateofbirth']);
$dobday = $dateofbirth[0];
$dobmonth = $dateofbirth[1];
$dobyear = $dateofbirth[2];

出生日期存储为dd,mm,yyyy,对于其他所有内容,我可以称之为很好。我的问题是现在我正在尝试使用以下方法使用fputcsv创建CSV文件:

$result = mysqli_query($con, 'SELECT u.user_id, b.dateofbirth FROM Users u INNER JOIN Basic b USING (user_id) ORDER BY user_id DESC');
$fp = fopen('latest.csv', 'w');
fputcsv($fp, array('User ID', 'DOB' )); 
CSV

会生成,但对于 CSV 中的出生日期列,它输出为 "*a:3:{i:0;s:2:"03";i:1;s:2:"02";i:2;s:4:"1986";}*",因为它显然仍处于序列化状态。处理这些字段的最佳和/或最简单的方法是什么?

提前非常感谢。

你可以试试这个:

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=latest.csv");// Disable caching
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
header("Pragma: no-cache"); // HTTP 1.0
header("Expires: 0"); // Proxies
$fp = fopen('latest.csv', 'w');
while($row = mysqli_fetch_array($result)){
fputcsv($fp,array($row[0],$row[1]));
}
/

/或者你可以使用上面提到的 out 标题

或者为出生日期设置一些格式,例如:

$rowdob = date("M d, Y - g:i:s A", strtotime( $row[0] ) );

in while 循环并将$rowdob传递给 fputcsv();