PHP XML和数组互相转换

转换代码如下

    //数组转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;
    }

 

Array转XML

$money = 101;
$sender = "方倍工作室";
$obj2 = array();
$obj2['wxappid']             = APPID;
$obj2['mch_id']             = MCHID;
$obj2['mch_billno']            = MCHID.date('YmdHis').rand(1000, 9999);
$obj2['client_ip']            = $_SERVER['REMOTE_ADDR'];
$obj2['re_openid']             = $openid;
$obj2['total_amount']       = $money;
$obj2['min_value']             = $money;
$obj2['max_value']             = $money;
$obj2['total_num']             = 1;
$obj2['nick_name']          = $sender;
$obj2['send_name']          = $sender;
$obj2['wishing']            = "恭喜发财";
$obj2['act_name']              = "方倍工作室送红包";
$obj2['remark']              = "关注公众账号";

转换后

<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>

 

 

XML转为Array

<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[发放成功]]></return_msg>
<result_code><![CDATA[SUCCESS]]></result_code>
<err_code><![CDATA[MONEY_LIMIT]]></err_code>
<err_code_des><![CDATA[发放成功]></err_code_des>
<mch_billno><![CDATA[1235571234201605241651123985]]></mch_billno>
<mch_id>1235571234</mch_id>
<wxappid><![CDATA[wx1b7559b818e3c123]]></wxappid>
<re_openid><![CDATA[oiPuduGV7gJ_MOSfAWpVmhhgXh-U]]></re_openid>
<total_amount>1</total_amount>
</xml>

转换后

array(10) {
  ["return_code"]=>
  string(7) "SUCCESS"
  ["return_msg"]=>
  string(12) "发放成功"
  ["result_code"]=>
  string(7) "SUCCESS"
  ["err_code"]=>
  string(11) "MONEY_LIMIT"
  ["err_code_des"]=>
  string(12) "发放成功"
  ["mch_billno"]=>
  string(28) "1235571234201605241651123985"
  ["mch_id"]=>
  string(10) "1235571234"
  ["wxappid"]=>
  string(18) "wx1b7559b818e3c123"
  ["re_openid"]=>
  string(28) "oiPuduGV7gJ_MOSfAWpVmhhgXh-U"
  ["total_amount"]=>
  string(1) "1"
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值