Initiate CSV from an array
by barkkathulla[ Edit ] 2013-03-06 17:37:14
To create CSV from an 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;
}
$data = array(
array(1, 2, 4),
array('test string', 'test, literal, comma', 'test literal "quotes"'),
);
echo generateCsv($data);
A comma-separated values (CSV) file stores tabular data (numbers and text) in plain-text form.This should be created in an easier way by the above concept.