热门IT资讯网

php将xml转换为数组

发表于:2024-11-24 作者:热门IT资讯网编辑
编辑最后更新 2024年11月24日,函数:/*@desc:xml转数组@param data xml字符串@return arr 解析出的数组*/function xmltoarray($data){$obj = simplexml_l
  1. 函数:
    /*@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;}
  2. 测试:
    a. 代码:
     Forty What?JoeJaneI 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?"}
0