PHP自定义函数xmlToArray的实例解析

PHP自定义函数xmlToArray实例

将xml转为array

  1. /**
  2. * 作用:将xml转为array
  3. */
  4. function xmlToArray($xml) {
  5. //将XML转为array
  6. $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  7. return $array_data;
  8. }

效果:

  1. <xml>
  2. <appid>wxd930ea5d5a258f4f</appid>
  3. <mch_id>10000100</mch_id>
  4. <device_info>1000</device_info>
  5. <body>test</body>
  6. <nonce_str>ibuaiVcKdpRxkhJA</nonce_str>
  7. <sign>9A0A8659F005D6984697E2CA0A9CF3B7</sign>
  8. </xml>

转化成:

  1. Array
  2. (
  3. [appid] => wxd930ea5d5a258f4f
  4. [mch_id] => 10000100
  5. [device_info] => 1000
  6. [body] => test
  7. [nonce_str] => ibuaiVcKdpRxkhJA
  8. [sign] => 9A0A8659F005D6984697E2CA0A9CF3B7
  9. )