最近做一个项目,需要和其他的程序进行对接,xml是交互数据的首选,所以,我需要一个函数能把php的数组转换成xml字符串,又同时可以把字符串再还原回php数组,其实是很简单的递归调用,发出来备用
函数定义:
01.
/* 将数组格式化为XML字符串 */
02.
function
array2xml(
$array
,
$level
= 0){
03.
$return
=
''
;
04.
if
(
$level
== 0){
05.
$return
=
'<?xml version="1.0" encoding="utf-8" ?><root>'
;
06.
}
07.
foreach
(
$array
as
$key
=>
$item
){
08.
if
(!
is_array
(
$item
)){
09.
$return
.=
"<item key='{$key}'>{$item}</item>"
;
10.
}
else
{
11.
$return
.=
"<item key='{$key}'>"
;
12.
$return
.= array2xml(
$item
,
$level
+ 1);
13.
$return
.=
"</item>"
;
14.
}
15.
}
16.
if
(
$level
== 0){
17.
$return
.=
'</root>'
;
18.
}
19.
return
$return
;
20.
}
21.
22.
/* 辅助函数用来获取DOM跟节点 */
23.
function
getXmlRoot(
$xml
){
24.
$doc
=
new
DOMDocument();
25.
$doc
->loadXML(
$xml
);
26.
$root
=
$doc
->documentElement;
27.
return
$root
;
28.
}
29.
30.
/* 将被array2xml格式化的XML还原 */
31.
function
xml2array(
$xml
){
32.
$return_array
=
array
();
33.
foreach
(
$xml
->childNodes
as
$node
){
34.
$length
=
$node
->childNodes->length;
35.
$key
=
$node
->getAttribute(
'key'
);
36.
if
(
$length
== 1
and
$node
->firstChild->nodeType == XML_TEXT_NODE){
37.
$return_array
[
$key
] =
$node
->nodeValue;
38.
}
else
{
39.
$return_array
[
$key
] = xml2array(
$node
);
40.
}
41.
}
42.
return
$return_array
;
43.
}
使用方法:
1. array转换为xml
01.
$array
=
array
(
02.
'aaa'
=>
array
(
03.
'title'
=>
'random test'
,
04.
'value'
=> md5(microtime())
05.
),
06.
'bbb'
=>
array
(
07.
'title'
=>
'fixed test'
,
08.
'value'
=>
'2010-01-01'
09.
),
10.
'ccc'
=>
'this is c'
,
11.
'ddd'
=>
array
(
12.
array
(
13.
time()
14.
),
15.
array
(
16.
time()
17.
),
18.
array
(
19.
time()
20.
),
21.
array
(
22.
time()
23.
)
24.
)
25.
);
26.
27.
$xml
= array2xml(
$array
);
28.
echo
$xml
;
运行结果为:
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
- <
root
>
03.
- <
item
key
=
"aaa"
>
04.
<
item
key
=
"title"
>random test</
item
>
05.
<
item
key
=
"value"
>2880f538686cf239481a70366047a619</
item
>
06.
</
item
>
07.
- <
item
key
=
"bbb"
>
08.
<
item
key
=
"title"
>fixed test</
item
>
09.
<
item
key
=
"value"
>2010-01-01</
item
>
10.
</
item
>
11.
<
item
key
=
"ccc"
>this is c</
item
>
12.
- <
item
key
=
"ddd"
>
13.
- <
item
key
=
"0"
>
14.
<
item
key
=
"0"
>1268222560</
item
>
15.
</
item
>
16.
- <
item
key
=
"1"
>
17.
<
item
key
=
"0"
>1268222560</
item
>
18.
</
item
>
19.
- <
item
key
=
"2"
>
20.
<
item
key
=
"0"
>1268222560</
item
>
21.
</
item
>
22.
- <
item
key
=
"3"
>
23.
<
item
key
=
"0"
>1268222560</
item
>
24.
</
item
>
25.
</
item
>
26.
</
root
>
2. xml转换为array
01.
<?php
02.
$array
=
array
(
03.
'aaa'
=>
array
(
04.
'title'
=>
'random test'
,
05.
'value'
=> md5(microtime())
06.
),
07.
'bbb'
=>
array
(
08.
'title'
=>
'fixed test'
,
09.
'value'
=>
'2010-01-01'
10.
),
11.
'ccc'
=>
'this is c'
,
12.
'ddd'
=>
array
(
13.
array
(
14.
time()
15.
),
16.
array
(
17.
time()
18.
),
19.
array
(
20.
time()
21.
),
22.
array
(
23.
time()
24.
)
25.
)
26.
);
27.
28.
/* 使用方法 */
29.
$xml
= array2xml(
$array
);
30.
$array2
= xml2array(getXmlRoot(
$xml
));
31.
print_r(
$array
);
32.
echo
'<br><br>'
;
33.
print_r(
$array2
);
34.
exit
();
35.
?>
运行结果为:
1.
Array ( [aaa] => Array ( [title] => random test [value] => fb5519e565162c6ac69700bb4c4a13c2 ) [bbb] => Array ( [title] => fixed test [value] => 2010-01-01 ) [ccc] => this is c [ddd] => Array ( [0] => Array ( [0] => 1268235843 ) [1] => Array ( [0] => 1268235843 ) [2] => Array ( [0] => 1268235843 ) [3] => Array ( [0] => 1268235843 ) ) )
2.
3.
Array ( [aaa] => Array ( [title] => random test [value] => fb5519e565162c6ac69700bb4c4a13c2 ) [bbb] => Array ( [title] => fixed test [value] => 2010-01-01 ) [ccc] => this is c [ddd] => Array ( [0] => Array ( [0] => 1268235843 ) [1] => Array ( [0] => 1268235843 ) [2] => Array ( [0] => 1268235843 ) [3] => Array ( [0] => 1268235843 ) ) )
联系客服