細細.魚 发表于 2018-12-12 07:52:27

php将xml转换为数组


[*]函数:
/*
@desc:xml转数组
@param data xml字符串
@return arr 解析出的数组
*/
function xmltoarray($data){
$obj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
$json = json_encode($obj);
$arr = json_decode($json, true);      
return $arr;
}
[*]测试:
a. 代码:


Forty What?
Joe
Jane

I know that's the answer -- but what's the question?


XML;
$arr = xmltoarray($string);
var_dump($arr);
  b. 输出:

array(4) {
["title"]=>
string(11) "Forty What?"
["from"]=>
string(3) "Joe"
["to"]=>
string(4) "Jane"
["body"]=>
string(57) "
I know that's the answer -- but what's the question?
"
}



页: [1]
查看完整版本: php将xml转换为数组