Hello reader! If you have an array with the records and you want it to be download as CSV format. PHP has built in functions for making the format of CSV. Lets take the example below:-
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
// Disable caching
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
header("Pragma: no-cache"); // HTTP 1.0
header("Expires: 0"); // Proxies
function outputCSV($ArrrayData) {
$output = fopen("php://output", "w");
foreach ($ArrrayData as $count) {
fputcsv($csv, $count);
}
fclose($csv);
}
csvCSV(array(
array("match 2nd", "score 123", "Over 25"),
array("match 3rd", "score 252", "Over 26"),
array("match 4th", "score 158", "Over 50")
));
On execution of above code you'll receive CSV
0 Comment(s)