这篇文章主要介绍使用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”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。