Created
September 19, 2013 07:20
-
-
Save aslamdoctor/6620056 to your computer and use it in GitHub Desktop.
Generate CSV file from Array using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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; | |
} | |
$data = array( array(1, 2, 4), array('test string', 'test, literal, comma', 'test literal "quotes"'), ); | |
echo generateCsv($data); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment