PHP 生成 CSV 文件-csv文件

<?php
$csv = "";
foreach ($data as $row) {
$csv .= join(",", $row) . "n";
}
echo $csv;
?>

<?php
function toCSV(array $data, array $colHeaders = array(), $asString = false) {
$stream = ($asString)
? fopen("php://temp/maxmemory", "w+")
: fopen("php://output", "w");

if (!empty($colHeaders)) {
fputcsv($stream, $colHeaders);
}

foreach ($data as $record) {
fputcsv($stream, $record);
}

if ($asString) {
rewind($stream);
$returnVal = stream_get_contents($stream);
fclose($stream);
return $returnVal;
}
else {
fclose($stream);
}
}

PHP 生成 CSV 文件

推荐阅读