Recursive implementation of PHP array to XML, PHP array xml recursion
PHP winning array to XML requirements are common, and there are many ways to implement, Baidu found a variety of implementation methods, but the basic is to borrow a group of components what. I wrote it myself. A string-grouping method that supports multidimensional arrays. For informational purposes only, please do not hesitate to enlighten me!
/** * Convert an array to xml* @param array $data the arrays to be converted * @param bool $root whether to root node * @return String XML String * @ Author dragondean* @url Http://www.cnblogs.com/dragondean*/functionArr2xml ($data,$root=true){ $str=""; if($root)$str.= "
"
;
foreach(
$data
as
$key = +
$val
) {
if(
I S_array(
$val
)) {
$child = arr2xml (
$val,
false c15>);
$str . = "<
$key>
$child
$key >"
;
}
Else
{
$str. = "<
$key> <span> $val
$key >"
; }}
if(
$root)
$str . = "
"; return $str;}
Above is the implementation of the method, the first parameter is the array you want to convert, the second optional parameter to set whether to add the root node, the default is required.
Test code:
$arr=array(' a ' = = ' aaa ', ' b ' = =array(' c ' = = ' 1234 ', ' d ' = ' = ' asdfasdf ')); echo arr2xml ($arr);
The result of the code execution is:
< XML >< a >
[cdata[aaa]]>
a><b >< C >
[cdata[1234]]>
c><D >
[CDATA[ASDFASDF]]>
D >
b >
XML >
---------------------- ----------
Update:
An array conversion in the following format was found to be problematic during use:
Array (Array(' title ' = ' Qwe ',' description ' = > ' rtrt ', ' picurl ' = ' sdfsd ', ' url ' = ' ghjghj ' ), array( ' title ' = ' Jyutyu ', ' description ' = ' werwe ', ' picurl ' = ' xcvxv ', ' url ' = ' Ghjgh ')) ;
The result of the conversion is:
<XML> <Item> <0> <title> </span><span>qwe</span> <span>
title> <Description> </span> <span>rtrt</span><span>
Description> <Picurl> </span> <span>sdfsd</span><span>
Picurl> <URL> </span> <span>ghjghj</span><span>
URL>
0> <1> <title> </span> <span>jyutyu</span><span>
title > < Description > </span> <span>werwe</span><span>
description> <picurl > </span> <span>XCVXV</span> <span>
picurl> <URL > </span> <span>ghjgh</span><span>
url>
1>
item>
xml>
Normally, the above-converted XML integer <0><1> that layer node is not for us. However, the subscript in PHP cannot have the same name and cannot have more than one item. What do we do?
I think one way is to give the item subscript, such as item[0],item[1], in the conversion process in the removal of the [] form of subscript, the implementation of multiple item nodes side-by.
The function is modified as follows:
functionArr2xml ($data,$root=true){ $str=""; if($root)$str.= "
"
;
foreach(
$data
as
$key = +
$val
) {
//
remove Ke Subscript in Y []
$key =
preg_replace('/\[\d*\]/', ' ',
$key
);
if(
is_array(
$val
)) {
$child = arr2xml (
$v Al,
false
);
$str . = "<
$key>
$child
$key >"
;
}
Else
{
$str. = "<
$key> <span> $val
$key >"
; }}
if(
$root)
$str . = "
"; return $str;}
Then the array that needs to be converted must also be changed:
$arr 1 = array (' title ' = = ' qwe ', ' description ' = > ' rtrt ', ' picurl ' = ' sdfsd ', ' url ' = ' ghjghj ' ), array( ' title ' = ' Jyutyu ', ' description ' = ' werwe ', ' picurl ' = ' xcvxv ', ' url ' = ' Ghjgh ' ));
The converted XML is as follows:
<XML> <Item> <title> </span> <span>qwe</span><span>
title> <Description> </span> <span>rtrt</span><span>
Description> <Picurl> </span> <span>sdfsd</span><span>
Picurl> <URL> </span> <span>ghjghj</span><span>
URL> !-- Item ; < Item ; < title ; ! [cdata[ jyutyu ]]> !-- title ; & lt; Description ; ! [cdata[ werwe ]]> !-- description ; < Picurl ; ! [cdata[ xcvxv ]]> !-- picurl ; < url ;
ghjgh ]]> !-- url ; -- item , !-- xml ;
http://www.bkjia.com/PHPjc/1001463.html www.bkjia.com true http://www.bkjia.com/PHPjc/1001463.html techarticle PHP Array to XML recursive implementation, PHP array xml recursive PHP winning array to XML is common, and there are many ways to implement, Baidu found a variety of implementation methods, but the base ...