*将xml数据转换成数组 * @param string $xmlstr xml格式的字符串 */ public function xmlToArray($xmlstr){ return json_decode(json_encode((array) simplexml_load_string($xmlstr)), true); }
其中:simplexml_load_string() 函数把 XML 字符串载入对象中。而后使用array强制转换成数组。可是因为载入的对象中节点层次比较深。array强制转换可能只把外层节点转换成数组,内层节点仍是对象。 因此,咱们用json_encode()函数转换成json数据。而后使用json_decode()将json数据返回回去(第二次参数设为true)当该参数为 TRUE时,将返回 array而非object) php