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!