Skip to content

Instantly share code, notes, and snippets.

@kasperhartwich
Last active December 9, 2015 22:28
Show Gist options
  • Select an option

  • Save kasperhartwich/4337223 to your computer and use it in GitHub Desktop.

Select an option

Save kasperhartwich/4337223 to your computer and use it in GitHub Desktop.
<?php
function arrayToXml($array, $rootElement = null, $xml = null) {
$_xml = $xml;
if ($_xml === null) {
$_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<command/>');
}
foreach ($array as $k => $v) {
if (is_array($v)) {
arrayToXml($v, $k, $_xml->addChild($k));
} else {
if ($v === NULL) {
$_xml->addChild($k);
} else if ($k === 0) {
$_xml->addChild($v);
} else {
$_xml->addChild($k, $v);
}
}
}
return $_xml->asXML();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment