I need to save data array to CSV file. The problem is the following: When I open CSV file, I see that 0 row of an array is saved at the same row as a header. Also the last column of a header contains 0, i.e. 'www0'. How to avoid this?
header("Content-type: text/csv");
header("Pragma: no-cache");
saveCSV($solutionCSV);
function saveCSV($data) {
$outstream = fopen("schedule.csv", "a");
$headers = 'xxx, yyy, zzz, www';
fwrite($outstream,$headers);
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals);
}
array_walk($data, "__outputCSV", $outstream);
fclose($outstream);
}
foreachwould be better thanarray_walk(), especially if you're not using an anonymous function, since a second call tosaveCSV()will cause a fatal error along the lines ofCannot redeclare function __outputCSV(), already declared in.... Aforeachwill be shorter, easier to understand when you come back to the code a year from now and more efficient.