Skip to content

Instantly share code, notes, and snippets.

@nfcg
Last active May 26, 2022 15:50
Show Gist options
  • Save nfcg/9af28292b687364590bdf50db373e5ae to your computer and use it in GitHub Desktop.
Save nfcg/9af28292b687364590bdf50db373e5ae to your computer and use it in GitHub Desktop.
php array to xml
<?php
function arrayToXML($array, SimpleXMLElement $xml, $child_name)
{
foreach ($array as $k => $v) {
if(is_array($v)) {
(is_int($k)) ? arrayToXML($v, $xml->addChild($child_name), $v) : $this->arrayToXML($v, $xml->addChild(strtolower($k)), $child_name);
} else {
(is_int($k)) ? $xml->addChild($child_name, $v) : $xml->addChild(strtolower($k), $v);
}
}
return $xml->asXML();
}
header("Content-Type: application/xml; charset=utf-8");
echo arrayToXML($My_Array, new SimpleXMLElement('<element/>'), 'child_name');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment