PHP自定义函数xmlToArray的实例解析
PHP自定义函数xmlToArray实例
将xml转为array
- /**
- * 作用:将xml转为array
- */
- function xmlToArray($xml) {
- //将XML转为array
- $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
- return $array_data;
- }
效果:
- <xml>
- <appid>wxd930ea5d5a258f4f</appid>
- <mch_id>10000100</mch_id>
- <device_info>1000</device_info>
- <body>test</body>
- <nonce_str>ibuaiVcKdpRxkhJA</nonce_str>
- <sign>9A0A8659F005D6984697E2CA0A9CF3B7</sign>
- </xml>
转化成:
- Array
- (
- [appid] => wxd930ea5d5a258f4f
- [mch_id] => 10000100
- [device_info] => 1000
- [body] => test
- [nonce_str] => ibuaiVcKdpRxkhJA
- [sign] => 9A0A8659F005D6984697E2CA0A9CF3B7
- )