Php array to xml recursion implementation, php array xml recursion. Php array to xml recursive implementation, php array xml recursive PHP winning array to xml is a common requirement, and there are many implementation methods, Baidu looked for a variety of implementation methods, but the php array is converted to xml recursion, and php array xml recursion.
The PHP winning array to xml is a common requirement, and there are also many implementation methods. baidu has looked at various implementation methods, but it basically relies 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
--------------------------------
Update:
During use, it is found that the following format array conversion may cause problems:
array( 'item' => array( array( 'title' => 'qwe', 'description' => 'rtrt', 'picurl' => 'sdfsd', 'url' => 'ghjghj' ), array( 'title' => 'jyutyu', 'description' => 'werwe', 'picurl' => 'xcvxv', 'url' => 'ghjgh' ) ));
The converted result is:
<0>
<![CDATA[qwe]]>
rtrt
sdfsd
ghjghj
<1>
<![CDATA[jyutyu]]>
werwe
xcvxv
ghjgh
Generally, we do not want the transformed xml <0> <1> layer nodes. However, in php, there cannot be multiple items with the same name. What should we do?
I figured out a way to subscripts to items, such as item [0] and item [1]. in the conversion process, the subscripts in the [] form are removed to implement multiple item nodes side by side.
The function is modified as follows:
Function arr2xml ($ data, $ root = true) {$ str = ""; if ($ root) $ str. ="
"; Foreach ($ data as $ key => $ val) {// remove the subscript [] $ key = preg_replace ('/\ [\ d * \]/' in the key, '', $ key); if (is_array ($ val) {$ child = arr2xml ($ val, false); $ str. = "<$ key> $ child
";} Else {$ str. =" <$ key>$val
";}} If ($ root) $ str. ="
"; Return $ str ;}
The array to be converted above also needs to be changed:
$arr1 =array( 'item[0]' => array( 'title' => 'qwe', 'description' => 'rtrt', 'picurl' => 'sdfsd', 'url' => 'ghjghj' ), 'item[1]' => array( 'title' => 'jyutyu', 'description' => 'werwe', 'picurl' => 'xcvxv', 'url' => 'ghjgh' ));
The converted xml is as follows:
<![CDATA[qwe]]>
rtrt
sdfsd
ghjghj
<![CDATA[jyutyu]]>
werwe
xcvxv
ghjgh
It is common to convert the winning arrays of PHP into xml, and there are many implementation methods. baidu has found various implementation methods, but the basic...