數組轉xml

$params = [
            'appid'=> self::APPID,
            'mch_id'=> self::MCHID,
            'nonce_str'=>md5(time()),
            'body'=>'公衆號支付測試',
            'out_trade_no'=>$oid,
            'total_fee'=> 2,
            'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],
            'notify_url'=> 'https://www.liminghulian.com/test/notify.php',
            'trade_type'=>'JSAPI',
            'product_id'=>$oid,
            'openid'    => $this->getOpenId()
        ];


//數組轉xml
    public function ArrToXml($arr)
    {
        if(!is_array($arr) || count($arr) == 0) return '';

        $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;
    }
相關文章
相關標籤/搜索