Before I begin, I know there are a bunch of questions similar to this, but trust me, I read all, if not most of them. I tried a bunch of solutions, but none of them seem to
I had problems with numeric tags using Ghost solution. I had to changed it a bit to remove the numeric tags (just adding a n before):
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement(' ');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild(is_numeric((string) $key)?("n".$key):$key));
} else {
$xml->addChild(is_numeric((string) $key)?("n".$key):$key, $value);
}
}
return $xml->asXML();
}
Anyways it does not use attributes, and it does not make arrays with numbers as nodes with same name. I have changed it a bit more to:
function array2xml($array, $parentkey="", $xml = false){
if($xml === false){
$xml = new SimpleXMLElement(' ');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, is_numeric((string) $key)?("n".$key):$key, $xml->addChild(is_numeric((string) $key)?$parentkey:$key));
} else {
$xml->addAttribute(is_numeric((string) $key)?("n".$key):$key, $value);
}
}
return $xml->asXML();
}
Changing the call to
$xml = array2xml($jSON, "", false);
With this you get a more natural xml output using attributes and having nodes with same name.