I am trying to write a .csv file with the following function that is described here:
This is the function:
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen('/path/to/file.csv', 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
And I call it like so:
array2csv($array);
Where $array[0]
(all other values follow this format) is as follows:
array(10) {
[1]=>
string(19) "2015-03-01 00:09:01"
[2]=>
string(11) "1234567890"
[3]=>
string(2) "26"
[7]=>
string(7) "0.10102"
[8]=>
string(12) "TESTING THIS"
[9]=>
}
I have been battling for a while now trying to figure out why I get output as follows within the .csv file:
0,1,2,3,4,5,6,7,8,9
"2015-03-01 00:09:01",1234567890,26,0.10102,"TESTING THIS"
... etc ...
Have not really seen any other issues like this while doing research and I do not understand myself why this is happening.
Could anyone enlighten me as to what is going on here? Thank you!