Created
October 12, 2012 06:54
-
-
Save mtdowling/3877687 to your computer and use it in GitHub Desktop.
XML to array using json_encode
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
<?php | |
// Note that this will not maintain XML attributes | |
function jsonXml2Array(SimpleXMLElement $xml) | |
{ | |
return json_decode(json_encode($xml), true); | |
} | |
function jsonXml2ArrayWithNamespacedAttributes($xml) | |
{ | |
// Gets rid of all namespace definitions and namespace references | |
return json_decode(json_encode(new \SimpleXMLElement( | |
preg_replace(array('/\sxmlns[^"]+"[^"]+"/', '/[a-zA-Z]+:([a-zA-Z]+[=>])/'), array('', '$1'), $xml) | |
)), true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment