Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$headers = array();
foreach ($xml->ROW->children() as $field) {
$headers[] = $field->getName();
}
$csv_filename = str_replace('xml', 'csv', $filename);
$file = $this->getCsvDirectory() . '/' . $csv_filename;
if (file_exists($file)) {
unlink($file);
}
$csv = fopen($file, 'w');
fputcsv($csv, $headers, ',', '"');
foreach ($xml as $entry) {
$data = get_object_vars($entry);
// Decode HTML entities.
$sanitized_data = array();
foreach ($data as $key => $datum) {
$sanitized_data[$key] = html_entity_decode($datum, ENT_COMPAT, 'UTF-8');
}
fputcsv($csv, $sanitized_data, ',', '"');
}
fclose($csv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment