从一个PHP数组生成CSV文件的代码
时间:2021-07-01 10:21:17
帮助过:30人阅读
下面这段代码能够从PHP数组生成一个CSV文件。- function generateCsv($data, $delimiter = ',', $enclosure = '"') {
- $handle = fopen('php://temp', 'r+');
- foreach ($data as $line) {
- fputcsv($handle, $line, $delimiter, $enclosure);
- }
- rewind($handle);
- while (!feof($handle)) {
- $contents .= fread($handle, 8192);
- }
- fclose($handle);
- return $contents;
- }
|
PHP, CSV