天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

探討:array2xml和xml2array以及xml與array的互相轉(zhuǎn)化

php在做后臺(tái)服務(wù)器的時(shí)候,經(jīng)常會(huì)遇到這種情況,需要解析來自前臺(tái)的xml文件,并將數(shù)據(jù)以xml格式返回,在這種情況下,xml與php中關(guān)聯(lián)數(shù)組的轉(zhuǎn)化是非常頻繁的事情。比如flex和其他客戶端程序與服務(wù)器的交互,經(jīng)常會(huì)使用這種方法。下面是我歸納的兩個(gè)方法,大大簡(jiǎn)化了xml與數(shù)組相互轉(zhuǎn)化的工作量。
復(fù)制代碼 代碼如下:
/**
     *
     * 將簡(jiǎn)單數(shù)組轉(zhuǎn)化為簡(jiǎn)單的xml
     * @param string $data  要進(jìn)行轉(zhuǎn)化的數(shù)組
     * @param string $tag   要使用的標(biāo)簽
     * @example
     * $arr = array(
        'rtxAccount'=>'aaron','ipAddr'=>'192.168.0.12',
        'conferenceList'=>array('conference'=>
                            array(
                                array('conferenceId'=>1212,'conferenceTitle'=>'quanshi 444','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>454,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>6767,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>232323,'conferenceTitle'=>'quanshi uuu','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>8989,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>1234343212,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.NET')
                                )
                            )
        );
        轉(zhuǎn)化為:
        <rtxAccount>aaron</rtxAccount>
        <ipAddr>192.168.0.12</ipAddr>
        <conferenceList>
            <conference>
                <conferenceId>1212</conferenceId>
                <conferenceTitle>quanshi 444</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>454</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>6767</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>232323</conferenceId>
                <conferenceTitle>quanshi uuu</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>8989</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>1234343212</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
        </conferenceList>
     */
    function array2xml($data,$tag = '')
    {
        $xml = '';

        foreach($data as $key => $value)
        {
            if(is_numeric($key))
            {
                if(is_array($value))
                {
                    $xml .= "<$tag>";
                    $xml .= array2xml($value);
                    $xml .="</$tag>";
                }
                else
                {
                    $xml .= "<$tag>$value</$tag>";
                }   
            }
            else
            {
                if(is_array($value))
                {
                    $keys = array_keys($value);
                    if(is_numeric($keys[0]))
                    {
                        $xml .=array2xml($value,$key);
                    }
                    else
                    {
                        $xml .= "<$key>";
                        $xml .=array2xml($value);
                        $xml .= "</$key>";
                    }

                }
                else
                {
                    $xml .= "<$key>$value</$key>";
                }
            }
        }
        return $xml;
    }            
}

xml2array
復(fù)制代碼 代碼如下:
/**
  *
  * 將簡(jiǎn)單的xml轉(zhuǎn)化成關(guān)聯(lián)數(shù)組
  * @param string $xmlString  xml字符串
  * @example
  * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RTXConferenceReqDTO>
 <conferenceTitle>IT交流會(huì)</conferenceTitle>
 <startTime>2011-12-19 12:00:00</startTime>
 <rtxAccount>andy1111111</rtxAccount>
 <ipAddr>192.168.1.56</ipAddr>
 <duration>120</duration>
 <conferenceType>1</conferenceType>
 <invitees>
  <invitee>
   <rtxAccount>被邀請(qǐng)人1的RTX賬號(hào)</rtxAccount>
   <tel>被邀請(qǐng)人1電話號(hào)碼</tel>
  </invitee>
  <invitee>
   <rtxAccount>被邀請(qǐng)人2的RTX賬號(hào)</rtxAccount>
   <tel>被邀請(qǐng)人2電話號(hào)碼</tel>
  </invitee>
 </invitees>
</RTXConferenceReqDTO>
轉(zhuǎn)化之后的關(guān)聯(lián)數(shù)組:
Array
(
    [conferenceTitle] => IT交流會(huì)
    [startTime] => 2011-12-19 12:00:00
    [rtxAccount] => andy1111111
    [ipAddr] => 192.168.1.56
    [duration] => 120
    [conferenceType] => 1
    [invitees] => Array
        (
            [invitee] => Array
                (
                    [0] => Array
                        (
                            [rtxAccount] => 被邀請(qǐng)人1的RTX賬號(hào)
                            [tel] => 被邀請(qǐng)人1電話號(hào)碼
                        )
                    [1] => Array
                        (
                            [rtxAccount] => 被邀請(qǐng)人2的RTX賬號(hào)
                            [tel] => 被邀請(qǐng)人2電話號(hào)碼
                        )
                )
        )
)
  */
 function xml2array($xmlString = '')
 {
  $targetArray = array();
  $xmlObject = simplexml_load_string($xmlString);
  $mixArray = (array)$xmlObject;
  foreach($mixArray as $key => $value)
  {
   if(is_string($value))
   {
    $targetArray[$key] = $value;
   }
   if(is_object($value))
   {
    $targetArray[$key] = xml2array($value->asXML());
   }
   if(is_array($value))
   {
    foreach($value as $zkey => $zvalue)
    {
     if(is_numeric($zkey))
     {
      $targetArray[$key][] = xml2array($zvalue->asXML());
     }
     if(is_string($zkey))
     {
      $targetArray[$key][$zkey] = xml2array($zvalue->asXML());
     }
    }
   }
  }
  return $targetArray;

 }

php技術(shù)探討:array2xml和xml2array以及xml與array的互相轉(zhuǎn)化,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 蜜柚视频高清在线 | 欧美日韩一级黄色片 | 97精品视频在线观看 | 99国产精品白浆在线观看免费 | 秋霞电影网午夜鲁丝片 | 午夜理论电影在线观看亚洲 | 中文乱码35页在线观看 | 日本人作爰啪啪全过程 | 青青草视频在线ac | 国产偷啪自怕网 | 综合精品欧美日韩国产在线 | 久久综合亚洲色hezyo | 同桌别揉我奶了嗯啊 | 玩50岁四川熟女大白屁股直播 | 亚洲国产在线精品国自产拍五月 | 高清不卡伦理电影在线观看 | 国产亚洲国际精品福利 | 三级黄色在线视频中文 | 中文字幕人成人乱码亚洲AV | 午夜福利院电影 | 午夜日韩久久影院 | 九色PORNY丨视频入口 | 久久久久综合一本久道 | 十分钟免费观看大全视频 | 欧美午夜精品久久久久久浪潮 | 最近中文字幕2019免费版日本 | 久久a级片 | 日韩精品欧美在线视频在线 | 国产盗摄一区二区 | 99亚洲精品自拍AV成人软件 | 免费精品国产人妻国语麻豆 | 成人在线高清不卡免费视频 | 色狠狠一区二区 | 免费看大黄高清网站视频在线 | 日本乱子人伦在线视频 | 国产GV无码A片在线观看 | 国产精品自产拍在线观看网站 | 久久久久久久国产精品视频 | 国产色婷婷精品人妻蜜桃成熟时 | 亚洲中字幕永久在线观看 | 男女AA片免费 |