php xml与数组的转换函数
function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } //将XML转为array function xmlToArray($xml) { //禁止引用外部xml实体 libxml_disable_entity_loader(true); $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $values; }
$obj2="<xml> <wxappid><![CDATA[wx1b7559b818e3c123]]></wxappid> <mch_id>1235571234</mch_id> <mch_billno>1235571234201605241726128109</mch_billno> <client_ip><![CDATA[127.0.0.1]]></client_ip> <re_openid><![CDATA[oiPuduGV7gJ_MOSfAWpVmhhgXh-U]]></re_openid> <total_amount>101</total_amount> <min_value>101</min_value> <max_value>101</max_value> <total_num>1</total_num> <nick_name><![CDATA[方倍工作室]]></nick_name> <send_name><![CDATA[方倍工作室]]></send_name> <wishing><![CDATA[恭喜发财]]></wishing> <act_name><![CDATA[方倍工作室送红包]]></act_name> <remark><![CDATA[关注公众账号]]></remark> </xml>";
打印结果
Array ( [wxappid] => wx1b7559b818e3c123 [mch_id] => 1235571234 [mch_billno] => 1235571234201605241726128109 [client_ip] => 127.0.0.1 [re_openid] => oiPuduGV7gJ_MOSfAWpVmhhgXh-U [total_amount] => 101 [min_value] => 101 [max_value] => 101 [total_num] => 1 [nick_name] => 方倍工作室 [send_name] => 方倍工作室 [wishing] => 恭喜发财 [act_name] => 方倍工作室送红包 [remark] => 关注公众账号 )