I have a simple little script that grabs XML and converts it to CSV. However it doesn’t write the headers. Would anyone know how to write the headers?
$file='example.xml';
if (file_exists($file)) {
$xml = simplexml_load_file($file);
$f = fopen('newfile.csv', 'w');
foreach ($xml->Information as $information) {
fputcsv($f, get_object_vars($information),',','"');
}
fclose($f);
}
This would put all the content of the child elements of <Information> in nice columns for Excel. But it won’t write the element names as CSV headings.
Any ideas how I could edit the script to also include the headers?
Thanks
Mike
You can use the SimpleXMLElement::getName() function to get the element name from the first set of data and use that to write the CSV header.