php实现数组转化成xml的类
来源:互联网 发布:中科大 知乎 编辑:程序博客网 时间:2024/05/19 20:59
class ArrayUtility
{
/**
* Convert array to xml tree
*
* @param array $array
* @param array $options
* @return string
* @example xmlize()
*/
public function xmlize($array, $options)
{
$encoding = isset($options['encoding']) ? $options[$encoding] :'utf-8';
$root = isset($options['root']) ? $options['root'] : 'response';
$xml = "< ?xml version="1.0" encoding="{$encoding}"?> < $root> ”;
$xml .= self::_xmlize($array);
$xml .= “”;
return $xml;
}
private function _xmlize($array)
{
$string = ”;
foreach($array as $key => $value)
{
$stag = is_numeric($key) ? “item id=”$key”" : $key;
$etag = is_numeric($key) ? “item” : $key;
if (is_array($value))
{
$string .= “< " . $stag . ">” . self::_xmlize($value) . “ ”;
}
else
{
$string .= “< " . $stag . ">” . $value . “ ”;
}
}
return $string;
}
}
使用方法:
$api_data = array(
'status' => '200',
'message' => '成功',
'data' => array(
'account'=> array(
0 => array(
'id' => 15,
'plat' => 1,
'pay_account' => 'glemir@yahoo.cn'
),
1 => array(
'id' => 16,
'plat' => 2,
'pay_account' => 'test@gmail.com
)
)
)
);
header('Content-Type: text/xml');
$arrayob = new ArrayUtility();
$xml = $arrayob->xmlize($api_data); //这里可以加上编码的参数,默认utf-8
echo $xml;
{
/**
* Convert array to xml tree
*
* @param array $array
* @param array $options
* @return string
* @example xmlize()
*/
public function xmlize($array, $options)
{
$encoding = isset($options['encoding']) ? $options[$encoding] :'utf-8';
$root = isset($options['root']) ? $options['root'] : 'response';
$xml = "< ?xml version="1.0" encoding="{$encoding}"?> < $root> ”;
$xml .= self::_xmlize($array);
$xml .= “”;
return $xml;
}
private function _xmlize($array)
{
$string = ”;
foreach($array as $key => $value)
{
$stag = is_numeric($key) ? “item id=”$key”" : $key;
$etag = is_numeric($key) ? “item” : $key;
if (is_array($value))
{
$string .= “< " . $stag . ">” . self::_xmlize($value) . “ ”;
}
else
{
$string .= “< " . $stag . ">” . $value . “ ”;
}
}
return $string;
}
}
使用方法:
$api_data = array(
'status' => '200',
'message' => '成功',
'data' => array(
'account'=> array(
0 => array(
'id' => 15,
'plat' => 1,
'pay_account' => 'glemir@yahoo.cn'
),
1 => array(
'id' => 16,
'plat' => 2,
'pay_account' => 'test@gmail.com
)
)
)
);
header('Content-Type: text/xml');
$arrayob = new ArrayUtility();
$xml = $arrayob->xmlize($api_data); //这里可以加上编码的参数,默认utf-8
echo $xml;
- php实现数组转化成xml的类
- php对xml的数组转化
- php数组与xml的相互转化
- xml转化成php数组(文件夹读取,XML解析)
- 使用 PHP 将 XML 转化为易读的数组!
- php xml字符串转化为 array数组的代码
- php xml与数组相互转化
- php数组和xml之间相互转化
- PHP学习:数组的转化
- JQuery 将json 转化成xml 格式的数组
- stdClass对象数组转化成php数组
- Php的数组转换成XML(php Array to Xml)
- php 数组 转化 xml 和json 一个class api的福音哈
- [PHP] 原生PHP使用递归方式实现数组转换成XML的功能之思考
- PHP读取远程xml文件并转化为数组
- php json接口转化为数组 生成xml接口
- php 数组和字符串的转化
- 对象和数组的相互转化 php
- JSP中把动态页面转换为静态页面
- 用电脑的人最好吃什么
- 这些蔬菜不能搭配吃
- 基于ExtJS开发的单用户Blog系统文档及源码下载
- css固定定位,即悬浮效果
- php实现数组转化成xml的类
- dreamweaver在 ONLOAD运行_onopen.htm时,发生以下javascript错误
- [oracle]linux平台启动关闭oracle数据库
- 怎么屏蔽鼠标右键的限制
- Stuct, spring, hibernate集成开发备忘
- ASP的三级联动下拉菜单
- 将Activex,打包成CAB文件放在网页上供人下载
- 管道程序.很多书上的程序是错误的!
- IIS错误.COM Surrogate 遇到问题需要关闭