gpt4 book ai didi

php - 使用 fputcsv 将数组放入 CSV

转载 作者:行者123 更新时间:2023-11-29 23:11:20 27 4
gpt4 key购买 nike

我的表中有相当多的字段,我已将它们序列化以减少字段数量。对于在存储数据时完美运行的其他所有内容,以及在需要时我可以使用以下示例:

$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]));
}

//或者您可以不使用上面提到的 header

或设置出生日期的某种格式,例如:

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

在 while 循环中并将 $rowdob 传递到 fputcsv();

关于php - 使用 fputcsv 将数组放入 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28065428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com