Convert.class.php
1023 Bytes
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
<?php
/**
* 转换工具类
*
* @name Util_Utils_Convert
* @package util/utils
* @copyright yoho.inc
* @version 0.1 (2013-05-07 10:56:55)
* @author fei.hong <fei.hong@yoho.cn>
*/
class Util_Utils_Convert
{
/**
* 将Array转为Xml格式
*
* @param array $data (数组数据)
* @param string $node (节点名称)
* @param string $subnode (子节点名称)
* @return string
*/
public static function array2xml($data, $node = 'list')
{
$xml = '';
if (is_array($data))
{
$first = key($data);
foreach ($data as $key => $value)
{
if (is_numeric($key))
{
$key = $node;
}
$xml .= ($key !== $first) ? "\n" : "";
$xml .= "<{$key}>" . self::array2xml($value, $node) . "</{$key}>";
}
}
elseif (is_string($data))
{
$xml = htmlspecialchars($data, ENT_QUOTES);
}
else
{
$xml = (string) $data;
}
return $xml;
}
}