Php array to xml recursion implementation, 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. = "<xml>"; foreach ($ data as $ key => $ val) {if (is_array ($ val) {$ child = arr2xml ($ val, false ); $ str. = "<$ key> $ child </$ key>";} else {$ str. = "<$ key> <! [CDATA [$ val]> </$ key> ";}} if ($ root) $ str. =" </xml> "; return $ str ;}
The above is the implementation method. The first parameter is the array to be converted, and the second optional parameter is set to whether to add the <xml> 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:
<xml><a><![CDATA[aaa]]></a><b><c><![CDATA[1234]]></c><d><![CDATA[asdfasdf]]></d></b></xml>
--------------------------------
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:
<xml> <item> <0> <title> <![CDATA[qwe]]> </title> <description> <![CDATA[rtrt]]> </description> <picurl> <![CDATA[sdfsd]]> </picurl> <url> <![CDATA[ghjghj]]> </url> </0> <1> <title> <![CDATA[jyutyu]]> </title> <description> <![CDATA[werwe]]> </description> <picurl> <![CDATA[xcvxv]]> </picurl> <url> <![CDATA[ghjgh]]> </url> </1> </item></xml>
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. = "<xml>"; foreach ($ data as $ key => $ val) {// remove the subscript [] $ key = preg_replace ('/\ [\ d * \]/', '', $ key) in the key ); if (is_array ($ val) {$ child = arr2xml ($ val, false); $ str. = "<$ key> $ child </$ key>";} else {$ str. = "<$ key> <! [CDATA [$ val]> </$ key> ";}} if ($ root) $ str. =" </xml> "; 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:
<xml> <item> <title> <![CDATA[qwe]]> </title> <description> <![CDATA[rtrt]]> </description> <picurl> <![CDATA[sdfsd]]> </picurl> <url> <![CDATA[ghjghj]]> </url> </item> <item> <title> <![CDATA[jyutyu]]> </title> <description> <![CDATA[werwe]]> </description> <picurl> <![CDATA[xcvxv]]> </picurl> <url> <![CDATA[ghjgh]]> </url> </item></xml>