Last active
September 3, 2015 13:31
-
-
Save osde8info/1dec2af2fd67f17fb713 to your computer and use it in GitHub Desktop.
convert php simplexml xml node to consistent array or json for one or more children with values and attributes intact
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
/* | |
** http://stackoverflow.com/questions/23766159/php-simplexml-arrays-generated-differently-for-single-child-and-multiple-child | |
*/ | |
$out = array(); | |
foreach ($xmlobj->MainNode as $childnode) { | |
foreach ($childnode as $n) { | |
$e = new \SimpleXMLElement($n->asXML()); | |
$out[] = $e; | |
} | |
} | |
var_export(json_encode($out,JSON_PRETTY_PRINT)); | |
echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment