Recursive php array-to-xml code sharing

Source: Internet
Author: User
This article describes how to implement recursive escape of arrays in PHP in the form of examples and shares it with you for your reference. The specific method is as follows: PHP requires to convert arrays into xml, and there are many implementation methods. baidu has found various implementation methods, however, it is basically based on some components. I wrote a string concatenation method that supports multi-dimensional arrays. It is for reference only. if you have any shortcomings, please kindly advise me!

/*** Convert the array to xml * @ param array $ data the array to be converted * @ param bool $ whether the root node is required * @ return string xml string * @ author Dragondean * @ url http://www.cnblogs.com/dragondean#/function arr2xml ($ data, $ root = true) {$ str = ""; if ($ root) $ str. ="
 
  
"; Foreach ($ data as $ key => $ val) {if (is_array ($ val) {$ child = arr2xml ($ val, false); $ str. = "<$ key> $ child
  ";} Else {$ str. =" <$ key>$val
  ";}} If ($ root) $ str. ="
 "; Return $ str ;}

The above is the implementation method. The first parameter is the array you want to convert, and the Second Optional parameter is set whether to add Root node, which is required by default.

Test code:

$arr=array('a'=>'aaa','b'=>array('c'=>'1234' , 'd' => "asdfasdf"));echo arr2xml($arr);

The result after code execution is:

 aaa
  
   1234
   
   asdfasdf
   
 

The above is all the content of this article. I hope you will like it.


For more information about recursive php array-to-xml code, see The PHP Chinese website!

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.