Skip to content

Instantly share code, notes, and snippets.

@osde8info
Last active September 3, 2015 13:31
Show Gist options
  • Save osde8info/1dec2af2fd67f17fb713 to your computer and use it in GitHub Desktop.
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
/*
** 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