Skip to content

Instantly share code, notes, and snippets.

@weivall
Created November 11, 2013 22:38
Show Gist options
  • Save weivall/7421797 to your computer and use it in GitHub Desktop.
Save weivall/7421797 to your computer and use it in GitHub Desktop.
Generate CSV file from a PHP array
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment