php 怎么将数组转xml的函数
展开全部
没有现成函数,只能自己写;我有一个别人写的函数:
<?php
class A2Xml {
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'
);
$xml = new A2Xml();
echo $xml->toXml($res);
<?php
class A2Xml {
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'
);
$xml = new A2Xml();
echo $xml->toXml($res);
展开全部
php没有现成的函数,只能自己遍历数组再去拼xml
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<?php
class A2Xml {
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'
);
$xml = new A2Xml();
echo $xml->toXml($res);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询