Last active
May 26, 2022 15:50
-
-
Save nfcg/9af28292b687364590bdf50db373e5ae to your computer and use it in GitHub Desktop.
php array to xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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