推荐一个听书手机软件:Thinkly Blog ? 通用php数组(array)与xml相互转换函数
来源:百度文库 编辑:偶看新闻 时间:2024/05/02 14:13:45
最近做一个项目,需要和其他的程序进行对接,xml是交互数据的首选,所以,我需要一个函数能把php的数组转换成xml字符串,又同时可以把字符串再还原回php数组,其实是很简单的递归调用,发出来备用
函数定义:
view sourceprint?01.
/* 将数组格式化为XML字符串 */
02.
function
array2xml(
$array
,
$level
= 0){
03.
$return
=
''
;
04.
if
(
$level
== 0){
05.
$return
=
''
;
06.
}
07.
foreach
(
$array
as
$key
=>
$item
){
08.
if
(!
is_array
(
$item
)){
09.
$return
.=
"- {$item}
"
;
10.
}
else
{
11.
$return
.=
"- "
;
12.
$return
.= array2xml(
$item
,
$level
+ 1);
13.
$return
.=
""
;
14.
}
15.
}
16.
if
(
$level
== 0){
17.
$return
.=
''
;
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
view sourceprint?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
;
运行结果为:
view sourceprint?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
view sourceprint?01.
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
'
'
;
33.
print_r(
$array2
);
34.
exit
();
35.
?>
运行结果为:
view sourceprint?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 ) ) )
php的blog系统
PHP的BLOG,推荐一个!
php程序的blog哪些比较好?
PHP BLOG本机安装出错.
支持gmail blog即Gallina的php免费空间
支持gmail blog即Gallina的php免费空间
谁知道有什么好点的php单用户blog程序
ASP和PHP与blog有关吗?它们是什么概念?
大家都是用什么程序写blog的?PHP?
如何架设ASP,PHP,JSP论坛和Blog。
请问PHP中哪个BLOG程序比较好?插件多?
谁向我推荐一个好的最好开源的PHP+MYSQL Blog程序?
帮忙推荐一款PHP真正免费多用户的汉化好一点的BLOG程序.
脱衣求php形式的blog,最好是生成静态页面的那种
BLOG 现在是用PHP的好还是ASP的好?
目前最好最完善的ASP或者PHP的BLOG系统是什么?
大家说说哪种PHP CMS好?要有BLOG和论坛.
谁介绍最好用的ASP和PHP的BLOG系统
Blog ???
谁可以邀请我注册http://bbs.verycd.com/index.php?automodule=blog论坛啊?
php是什么
什么是php
PHP是什么意思
php是什么?