×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Bob Ray
Added: Jul 16, 2015 10:24 PM
Modified: Jul 16, 2015 10:34 PM
Views: 17
Tags: array csv file
Convert PHP associative array to a CSV file
  1. /* Original code by Mohit Madan @TheMohitMadan */
  2.  
  3.  function generateCsv($data, $delimiter = ',', $enclosure = '"') {
  4.        $handle = fopen('php://temp', 'r+');
  5.       foreach ($data as $line) {
  6.           fputcsv($handle, $line, $delimiter, $enclosure);
  7.    }
  8.    rewind($handle);
  9.    while (!feof($handle)) {
  10.        $contents .= fread($handle, 8192);
  11.    }
  12.    fclose($handle);
  13.    return $contents;
  14. }