最近做一個(gè)項(xiàng)目,需要和其他的程序進(jìn)行對(duì)接,xml是交互數(shù)據(jù)的首選,所以,我需要一個(gè)函數(shù)能把php的數(shù)組轉(zhuǎn)換成xml字符串,又同時(shí)可以把字符串再還原回php數(shù)組,其實(shí)是很簡(jiǎn)單的遞歸調(diào)用,發(fā)出來(lái)備用
函數(shù)定義:
01.
/* 將數(shù)組格式化為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.
/* 輔助函數(shù)用來(lái)獲取DOM跟節(jié)點(diǎn) */
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轉(zhuǎn)換為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
;
運(yùn)行結(jié)果為:
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轉(zhuǎn)換為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.
?>
運(yùn)行結(jié)果為:
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 ) ) )
聯(lián)系客服