-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayToXml.class.php
More file actions
64 lines (54 loc) · 1.79 KB
/
Copy pathArrayToXml.class.php
File metadata and controls
64 lines (54 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class ArrayToXml{
/*
* 数组转xml格式的函数 用于ajax的响应
* @param integer $status 处理状态
* @param string $message 提示信息 用于js的显示
* @param array $data 传给浏览器的数据
* return string
* author singwa
* modifier tinytoobad
* 20160424
* */
public static function array2xml($status=400, $message='fail', $data = array()) {
if(!is_numeric($status)) { return '@param status is not a integer'; }
$result = array(
'status' => $status,
'message' => $message,
'data' => $data,
);
// 指定页面显示类型,因在页面上显示故加上,若用于ajax可能应该用掉
header("Content-Type:text/xml");
// xml版本
$xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
$xml .= "<root>\n";
$xml .= self::array2xml_body($result);
$xml .= "</root>";
echo $xml;
}
/*
* 递归法,传入数组递归出xml的主体
* @param array $data 要转成xml主体的数组
* return string xml主体字符串
* author singwa
* modifier tinytoobad
* 20160424
* */
protected static function array2xml_body($data) {
$xml = $attr = "";
foreach($data as $key => $value) {
if(is_numeric($key)) {
$attr = " id='{$key}'";
$key = "item";
}
$xml .= "<{$key}{$attr}>";
$xml .= is_array($value) ? self::array2xml_body($value) : $value;
// 换行号实际用时应去掉
$xml .= "</{$key}>\n";
}
return $xml;
}
}
$arr = array('xx'=>['one','two','three'],'yy'=>'kk');
ArrayToXml::array2xml(200,'success',$arr);
?>