I want to convert a php array to xml.
I have this code:
$output=array("result" => "this is the result");
print assocArrayToXML("example",$output);
function assocArrayToXML($root_element_name,$ar) {
$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");
$f = create_function('$f,$c,$a','
foreach($a as $k=>$v) {
if(is_array($v)) {
$ch=$c->addChild($k);
$f($f,$ch,$v);
} else {
$c->addChild($k,$v);
}
}');
$f($f,$xml,$ar);
return $xml->asXML();
}
I found the function here:
http://php.net/manual/es/book.simplexml.php
When I execute the code, I got this:
this is the result
Why can't I see the xml? is it because it is malformed? or maybe the mozilla firefox doesn't write it?