php将xml转换为数组
发表于:2024-11-24 作者:热门IT资讯网编辑
编辑最后更新 2024年11月24日,函数:/*@desc:xml转数组@param data xml字符串@return arr 解析出的数组*/function xmltoarray($data){$obj = simplexml_l
- 函数:
/*@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. 代码: XML;$arr = xmltoarray($string);var_dump($arr);Forty What? Joe Jane I know that's the answer -- but what's the question?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?"}