gpt4 book ai didi

php - 创建一个数组数组以提供 CSV 函数

转载 作者:行者123 更新时间:2023-11-29 04:11:12 25 4
gpt4 key购买 nike

我正在尝试根据查询创建 CSV,我需要符合以下格式的数据:

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

$array = array(
array("data11", "data12", "data13"),
array("data21", "data22", "data23"),
array("data31", "data32", "data23"));
outputCSV($array);

我习惯于像这样聚合数据:

while ($row = mysql_fetch_array($result)){
$values[] = $row['value'];
}

如何传递 outputCSV 适当的数组?

最佳答案

function outputCSV($rows, $fieldNames = array())
{
if ($fp = fopen('php://output', 'w')) {
if ($fieldNames) {
fputcsv($fp, $fieldNames);
}
foreach ($rows as $row) {
fputcsv($fp, $row);
}
fclose($fp);
}
}

关于php - 创建一个数组数组以提供 CSV 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10709670/

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