PHP array to CSV Function


function convert_to_csv($input_array, $output_file_name, $delimiter){
$temp_memory = fopen('php://memory', 'w');
// loop through the array
foreach ($input_array as $line) {
// use the default csv handler
fputcsv($temp_memory, $line, $delimiter);
}fseek($temp_memory, 0);
// modify the header to be CSV format
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
// output the file to be downloaded
fpassthru($temp_memory);
}
convert_to_csv($array_of_data, 'data_as_csv.csv', ',');