I haven’t benchmarked the memory consumption of this method versus just traversing the array yourself and writing XML but I can’t see anything else being more efficient than this.
$testArray = [
'key1' => 'val1',
'key2' => [
'key3' => 'hello'
]
];
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($testArray, array ($xml, 'addChild'));
print $xml->asXML();
Leave a Reply