php 多维数组转xml的方法

public function arrToxml($arr,$dom = 0,$item=0){

        if(!$dom) $dom = new DOMDocument("1.0",'utf-8');
        $dom -> formatOutput = true;//格式xml输出 有换行什么的
        if(!$item){
            $item = $dom->createElement("AvailabilityRequest");
            $dom->appendChild($item);
            $item->setAttribute('session_id',session_id());//设置属性
        }

        foreach ($arr as $key=>$val){
            if (is_array($val)){

                $itemx = $dom->createElement($key);
                $item->appendChild($itemx);
                if(in_array($key,['Contact','Adult','Children'])){
                    if($key == 'Contact'){
                        foreach ($val as $k=>$v){
                            $itemx->setAttribute($k,$v);
                        }
                    }else{
                        $item->removeChild($itemx);
                        foreach ($val as $k=>$v){
                            $itemx = $dom->createElement($key);
                            $item->appendChild($itemx);
                            foreach ($v as $kk => $vv) {
                                $itemx->setAttribute($kk,$vv);
                            }
                        }
                    } 
                }else {
                    $this->arrToxml($val, $dom, $itemx);
                }


            }else {
                 $itemx = $dom->createElement($key,$val);
                 $item->appendChild($itemx);
            }
        }
        return $dom->saveXML();
    }

   $xmlArray = [
            'Authentication' => [
                'OrgID' => $orgId,
                'UserName' => $userName,
                'AuthKey' => $authKey,
                'Version' => $version
            ],
            'HotelRequest' => [
             'ReqType' => 'booking',
             'CityID' => '3',
             'CheckInDate' => '2016-03-10',
             'CheckOutDate' => '2016-03-11',
             'HotelID' => '2237',
             'RateCode' => 'CZWUyZTU1ZTljZDViMzc4ODdmZTZhNjZ',
             'RoomCount' => '1',
             'Nationality' => 'CN',
             'Adult' => '2',
             'Children' => '0',
             'ChildrenAge' => '22',
             'Currency' => 'CNY',
             'TotalAmount' => '963.36',
             'ConfirmType' => '1',
             'Contact' => [
                 'name' => 'ZHANG SAN',
                 'phone' => '13111111111',
                 'email' => '[email protected]',
                 'telephone' => '010-12341332'
             ],
             'Rooms' => [
                 'Room' => [
                     'Adult' =>[ 
                         [
                             'last' => 'ZHANG',
                             'first' => 'XIAOHONG'
                         ],
                         [
                             'last' => 'LI',
                             'first' => 'XIAOMING'
                         ],
                     ],
                     'Children' =>[ 
                         [
                             'last' => 'L3',
                             'first' => 'XIAOLIE'
                         ]
                     ]
                 ]
             ],
             'GuestRemarks' => '',
             'PartnerBookingID' => 'SN1234567890'
        ]

        ];
        $xmlData = $this->arrToxml($xmlArray);
        echo $xmlData;
        

    
        111
        222
        333
        1.0
    
    
        booking
        3
        2016-03-10
        2016-03-11
        2237
        CZWUyZTU1ZTljZDViMzc4ODdmZTZhNjZ
        1
        CN
        2
        0
        22
        CNY
        963.36
        1
        
        
            
                
                
                
            
        
        <!*CDATA*apply for two bed rooms++>
        SN1234567890
    

你可能感兴趣的:(php,xml)