xml,array的转换

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
//xml转为array
public function xmlToArray($xml)
{
$xml=simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$array_data = json_decode(json_encode($xml), true);
return $array_data;
}
//array转xml
public function arrayToXml($arr)
{
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">";
}
else
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml.="</xml>";
return $xml;
}

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2017 (ˇˍˇ) ~ All Rights Reserved.

(´▽`)ノ | (゚▽゚*)