• 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"
    }

     

  • 相关阅读:
    浅出Java Socket 编程
    WPF指南之一(WPF的结构)
    使用URL访问网络资源
    WPF指南之三(XAML的名字空间)
    多线程并发思考文件加锁
    关于kindeditor上传图片出现"服务器发生故障"的解决办法
    isset function of PHP
    JSON字符串传到后台PHP处理的问题
    isset function of PHP
    (转)Linux利器 strace
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6467364.html
Copyright © 2020-2023  润新知