Ask the great God array to convert the XML format. This post was last edited by HUENKE at 17:51:55, January 4 ,.
The content stored in PHP is a tree-like recursive array key => value
Convert to XML format
However
I need XML output in the following format
I debugged it for one afternoon. please help me
Function arrayToXml ($ arr, $ xmlDoc = 0, $ item = 0) {/* determine whether it is xml */if (! $ XmlDoc) {$ xmlDoc = new DOMDocument ("1.0");}/* determine whether it is a subnode */if (! $ Item) {$ item = $ xmlDoc-> createElement ("nodes", "UTF-8"); $ xmlDoc-> appendChild ($ item );} /* Press "key => value" for array data to retrieve and write the dom tree cyclically */foreach ($ arr as $ key => $ val) {/* Add the subnode name and value */$ itemX = $ xmlDoc-> createElement (is_string ($ key )? $ Key: "node"); $ item-> appendChild ($ itemX);/* recursion */if (! Is_array ($ val) {$ text = $ xmlDoc-> createTextNode ($ val); $ itemX-> appendChild ($ text );} else {$ this-> arrayToXml ($ val, $ xmlDoc, $ itemX ); // add $ this->}} return $ xmlDoc-> saveXML () to call the class. // return xml data, which can be directly written into the returned data *. xml file can be generated}
Reply to discussion (solution)
$ Ar = array ('id' => "c5569e2c-0f4a-4ec2-86ba-736b79eca18c", 'name' => "Company A", 'type' => "0 ", array ('id' => "4f2b55e9-e10a-496b-8bca-60e6f26daee5", 'name' => "department 1", 'type' => "1 ", array ('id' => "363", 'name' => "leaf 1", 'type' => "2", 'playurl' => "http: // XXXXXXX363 "),), array ('id' =>" 0c5ce42f-ba31-4b7a-8173-79ecae4a73ca ", 'name' =>" Department 2 ", 'type' =>" 1 ", array ('id' => "241", 'name' => "leaf 1", 'type' => "2", 'playurl' => "http: // xxxxx = 241 "), array ('id' =>" 356 ", 'name' =>" leaf 1 ", 'type' =>" 2 ", 'playurl' => "xxxxxxxxx = 356"),),); $ xml = simplexml_load_string ('
'); ArrayToXml ($ ar, $ xml); echo $ xml-> asXML (); function arrayToXml ($ ar, $ xml) {foreach ($ ar as $ k =>$ v) {if (is_array ($ v) arrayToXml ($ v, $ xml-> addChild ('node ')); else $ xml-> addAttribute ($ k, $ v );}}
Strategic mark
My ARRAY format is
The output format is
It seems that there is an extra layer of NODE. how can this problem be solved?