Skip to content

Instantly share code, notes, and snippets.

@mcunha98
Created August 13, 2021 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcunha98/3243003058dfccc9f142968164051ee7 to your computer and use it in GitHub Desktop.
Save mcunha98/3243003058dfccc9f142968164051ee7 to your computer and use it in GitHub Desktop.
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