使用php怎么将数组转化成xml

发布时间:2021-09-28 17:42:12 作者:小新
来源:亿速云 阅读:130

这篇文章主要介绍使用php怎么将数组转化成xml,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

使用php将数组转化成xml的方法:【class Array_to_Xml{private $version  = '1.0';private $encoding  = 'UTF-8';private $root    = 'ro...】。

本文操作环境:windows10系统、php 7、thinkpad t480电脑。

下面是使用php将数组转化为xml的具体实现代码,一起来看下吧。

具体实现代码:

<?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();//实例化类
echo $xml->toXml($res);//转为数组
?>

看下运行的效果:

使用php怎么将数组转化成xml

以上是“使用php怎么将数组转化成xml”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. php将xml转换为数组
  2. php将数组转化成字符串的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 数组 xml

上一篇:如何运用CentOS RPM安装CentOS 5.2中相关软件如vhmgr

下一篇:windows下如何安装php环境

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》