使用php怎么將數(shù)組轉(zhuǎn)化成xml

這篇文章主要介紹使用php怎么將數(shù)組轉(zhuǎn)化成xml,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

專業(yè)從事做網(wǎng)站、網(wǎng)站建設(shè),高端網(wǎng)站制作設(shè)計(jì),成都小程序開發(fā),網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團(tuán)隊(duì)竭力真誠服務(wù),采用html5+CSS3前端渲染技術(shù),成都響應(yīng)式網(wǎng)站建設(shè)公司,讓網(wǎng)站在手機(jī)、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項(xiàng)小組,與您實(shí)時在線互動,隨時提供解決方案,暢聊想法和感受。

使用php將數(shù)組轉(zhuǎn)化成xml的方法:【class Array_to_Xml{private $version  = '1.0';private $encoding  = 'UTF-8';private $root    = 'ro...】。

本文操作環(huán)境:windows10系統(tǒng)、php 7、thinkpad t480電腦。

下面是使用php將數(shù)組轉(zhuǎn)化為xml的具體實(shí)現(xiàn)代碼,一起來看下吧。

具體實(shí)現(xiàn)代碼:

<?php
class Array_to_Xml
{
    private $version  = '1.0';
    private $encoding  = 'UTF-8';
    private $root    = 'root';
    private $xml    = null;
    function __construct()
    {
        $this->xml = new XmlWriter();
    }
    function toXml($data, $eIsArray=FALSE)
    {
        if(!$eIsArray)
        {
            $this->xml->openMemory();
            $this->xml->startDocument($this->version, $this->encoding);
            $this->xml->startElement($this->root);
        }
        foreach($data as $key => $value)
        {
            if(is_array($value))
            {
                $this->xml->startElement($key);
                $this->toXml($value, TRUE);
                $this->xml->endElement();
                continue;
            }
            $this->xml->writeElement($key, $value);
        }
        if(!$eIsArray)
        {
            $this->xml->endElement();
            return $this->xml->outputMemory(true);
        }
    }
}
$res = array(
    'hello' => '11212',
    'world' => '232323',
    'array' => array(
        'test' => 'test',
        'b'  => array('c'=>'c', 'd'=>'d')
    ),
    'a' => 'haha'
);

header("Content-type:text/xml");//輸出xml頭信息
$xml = new Array_to_Xml();//實(shí)例化類
echo $xml->toXml($res);//轉(zhuǎn)為數(shù)組
?>

看下運(yùn)行的效果:

使用php怎么將數(shù)組轉(zhuǎn)化成xml

以上是“使用php怎么將數(shù)組轉(zhuǎn)化成xml”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前名稱:使用php怎么將數(shù)組轉(zhuǎn)化成xml
本文來源:http://muchs.cn/article4/ghooie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版營銷型網(wǎng)站建設(shè)、企業(yè)建站、App開發(fā)網(wǎng)站營銷、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)