PHP PHP 生成 CSV 文件

major · 2022-04-09 11:35:10 · 热度: 5

PHP 生成 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);  
    }  
}

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册