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

PHP array generate CSV file

WBOY
Release: 2016-07-25 08:45:20
Original
886 people have browsed it
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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template