The Array2xml class in PHP converts arrays into XML instances

高洛峰
Release: 2023-03-04 06:16:02
Original
980 people have browsed it

The example in this article describes the method of converting arrays into XML using the Array2xml class in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

<?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;
    }
}
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.


For more articles related to the Array2xml class in PHP that converts arrays into XML instances, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!