数组转XML
发表日期:2019-01-05 23:08:27 | 来源: | | 浏览(803) 分类:PHP杂项
<?php
/**
* 数组转XML
*/
class ArrayToXML
{
private $xmlString;
private function header()
{
$this->xmlString .= '<?xml version="1.0" encoding="utf-8" ?>';
$this->xmlString .= '<root>';
}
private function footer()
{
$this->xmlString .= '</root>';
}
public function encode($array)
{
header('Content-type:text/xml;');
$this->header();
$this->paser($array);
$this->footer();
return $this->xmlString ;
}
private function paser($array)
{
foreach ($array as $key => $value)
{
$tag = is_numeric($key) ? 'item' : $key;
$this->xmlString .= "<$tag";
if (is_array($value))
{
if (isset($value['id'])) $this->xmlString .= " id=\"{$value['id']}\"";
$this->xmlString .= ">";
$this->paser($value);
}
else
{
$this->xmlString .= ">";
$this->xmlString .= $value;
}
$this->xmlString .= "</$tag>";
}
}
}
$array = array(
'code' => 200,
'message' => 'succeess',
'data' => array(
array(
'id' => 1,
'title' => 'demo2',
),
array(
'id' => 2,
'title' => 'demo3',
)
)
);
$xml = new ArrayToXML;
echo $xml->encode($array);
?>
- PHP(0)
- PHP杂项(34)
- ip白黑名单验证(0)
- 自实现getallheaders函数 获取header信息(0)
- 随机颜色(0)
- 中文全角半角互转(0)
- 字符和unicode互转(0)
- 日期时间友好展示为今天/昨天/前天/分钟/小时/天前(0)
- 递归创建多级目录(0)
- 字符串截取追加...(0)
- 批量反/转义特殊字符,数据库安全过滤写入,支持字符串、多维数组、对象集合(0)
- 压缩HTML,去换行空格,注释(0)
- 获取文章html中的img标签图片src地址(0)
- html过滤标签空格提取纯文本text(0)
- 自实现http_build_url 函数(0)
- 获取浏览器类型版本及操作系统类型(0)
- curl请求及注意事项(0)
- 二维数组排序(0)
- 文件字节大小换算KB、MB、GB、TB(0)
- 数字金额转汉字大写(0)
- 验证身份证号是否有效(0)
- 批量替换文件名(0)
- 批量修改照片文件名日期排序(0)
- PHP中文编程(0)
- PHP命令行工具-在Linux下写shell脚本(0)
- usort-使用用户自定义的比较函数对数组中的值进行排序(0)
- PHP 使用 curl 提交 json 格式数据(0)
- tp+nginx(0)
- nginx配置(0)
- PHP测试mysql数据库MYSQLI和PDO的性能比较(0)
- PHP高级扩展介绍,高手必学(0)
- 数组转XML(0)
- Memcache和Redis的区别(0)
- PHP7.0新特性(0)
- PHP中的魔术方法总结 :__construct, __destruct等..(0)
- PHP配置文件php.ini详解(0)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)