Created
August 13, 2021 12:52
-
-
Save mcunha98/3243003058dfccc9f142968164051ee7 to your computer and use it in GitHub Desktop.
This file contains 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
function xml2array($xmlstring) | |
{ | |
$xml = simplexml_load_string($xmlstring); | |
$json = json_encode($xml); | |
return json_decode($json,TRUE); | |
} | |
function array2Xml($array, $rootElement = null, $xml = null) | |
{ | |
$_xml = $xml; | |
if ($_xml === null) | |
{ | |
$_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>'); | |
} | |
foreach ($array as $k => $v) | |
{ | |
if (is_array($v)) | |
{ | |
array2Xml($v, $k, $_xml->addChild($k)); | |
} | |
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