Home  >  Article  >  Backend Development  >  PHP array generate CSV file

PHP array generate CSV file

WBOY
WBOYOriginal
2016-07-25 08:45:20860browse
A very simple function that generates a .csv file from a PHP array. This function generates a comma-delimited file (.CSV) using the fputcsv PHP built-in function. This function has 3 parameters: data, delimiter and CSV enclosure. The default is double quotes.
  1. function generateCsv($data, $delimiter = ',', $enclosure = '"') {
  2. $handle = fopen('php://temp', 'r+');
  3. foreach ($data as $line) {
  4. fputcsv($handle, $line, $delimiter, $enclosure);
  5. }
  6. rewind($handle);
  7. while (!feof($handle)) {
  8. $contents .= fread($handle, 8192);
  9. }
  10. fclose($handle);
  11. return $contents;
  12. }
Copy code

PHP, CSV


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn