Home  >  Article  >  php教程  >  php里Array2xml

php里Array2xml

PHP中文网
PHP中文网Original
2016-05-25 17:13:211169browse

PHP代码

<?php
class Array2xml
{
    var $xml;
    function array2xml($array,$encoding=&#39;utf-8&#39;) {
        $this->xml=&#39;<?xml version="1.0" encoding="&#39;.$encoding.&#39;"?>&#39;;
        $this->xml.=$this->_array2xml($array);
    }
    function getXml() {
        return $this->xml;
    }
    function _array2xml($array)
    {
        $xml=&#39;&#39;;
        foreach($array as $key=>$val){
            if(is_numeric($key)){
                $key="item id=\"$key\"";
            }else{
                //去掉空格,只取空格之前文字为key
                list($key,)=explode(&#39; &#39;,$key);
            } 
            $xml.="<$key>";
            $xml.=is_array($val)?$this->_array2xml($val):$val;
            //去掉空格,只取空格之前文字为key
            list($key,)=explode(&#39; &#39;,$key);
            $xml.="</$key>";
        }
        return $xml;
    }
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn