This question already has an answer here:
I want to create a function for force download CSV file but the CSV file displays its content like print_r() I know this is not the right way but otherwise it doesn't display it at all.
function export_to_csv($result) {
$shtml = print_r($result);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="data.csv"');
echo $shtml;
}
This is how I created the CSV file through XML
function createCsv($xml, $f) {
foreach ($xml->children() as $item) {
$hasChild = (count($item->children()) > 0) ? true : false;
if (!$hasChild) {
if (!empty($item)) {
$put_arr = array($item->getName(), $item);
fputcsv($f, $put_arr, chr(9));
}
} else {
createCsv($item, $f);
}
}
}
Any ideas?
</div>