Ask the great God array to convert XML format

Source: Internet
Author: User
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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.