Transforming XML and Arrays

Source: Internet
Author: User
Tags addchild
Two methods recursive conversion of arrays and XML

 $V) {if ($v instanceof simplexmlelement| | Is_array ($v)) {$sim [$k]=toarray ($v);}} return $sim;} $sx =simplexml_load_file ('./try.xml ');p rint_r ($SX);p Rint_r (ToArray ($SX));//Look at the source code can see that the return of the page is Error $arr=array (' A ' = ' 111 ', ' b ' = ' 2222 ', ' C ' =>array (' d ' = ' = ' hahaha ', ' e ' =>array (' what ' = ' the ' x ')));//array to XML, new array added to new XML, Arr to Xmlfunction ToXml ($arr, $node =null) {//Create new XML file if ($node ==null) {$simxml =new simplexmlelement ('
 
 
  
 ');} else{$simxml = $node;} foreach ($arr as $k + $v) {if (Is_array ($v)) {//AddChild (node name, content)//If it is an array, build nodes to put things in this array-"V is the number group,//large sections name is K, to the next back , put things into this sim-"AddChild toxml ($v, $simxml->addchild ($k));//If the array key is a number, you should give him a label name, because the direct use of numbers will be problematic}else if (Is_numeric ($ k) {$simxml->addchild (' item ', $v);} else{//is not an array, just add the child element key name-key value $simxml->addchild ($k, $v);}} return $simxml->savexml ();} Header (' Content-type:text/xml '); Echo ToXml ($arr);? >

The above describes the transformation of XML and arrays, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.