array2xml xml2array
array2xml
/**
*
* 将简单数组转化为简单的xml
* @param string $data 要进行转化的数组
* @param string $tag 要使用的标签
* @example
* $arr = array(
'rtxAccount'=>'aaron','ipAddr'=>'192.168.0.12',
'conferenceList'=>array('conference'=>
array(
array('conferenceId'=>1212,'conferenceTitle'=>'quanshi
444','smeAccount'=>'http://www.jb51.net'),
array('conferenceId'=>454,'conferenceTitle'=>'quanshi
meetting','smeAccount'=>'http://www.jb51.net'),
array('conferenceId'=>6767,'conferenceTitle'=>'quanshi
meetting','smeAccount'=>'http://www.jb51.net'),
array('conferenceId'=>232323,'conferenceTitle'=>'quanshi
uuu','smeAccount'=>'http://www.jb51.net'),
array('conferenceId'=>8989,'conferenceTitle'=>'quanshi
meetting','smeAccount'=>'http://www.jb51.net'),
array('conferenceId'=>1234343212,'conferenceTitle'=>'quanshi
meetting','smeAccount'=>'http://www.jb51.net')
)
)
);
转化为:
<rtxAccount>aaron</rtxAccount>
<ipAddr>192.168.0.12</ipAddr>
<conferenceList>
<conference>
<conferenceId>1212</conferenceId>
<conferenceTitle>quanshi 444</conferenceTitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>454</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>6767</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>232323</conferenceId>
<conferenceTitle>quanshi uuu</conferenceTitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>8989</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
<conference>
<conferenceId>1234343212</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.jb51.net</smeAccount>
</conference>
</conferenceList>
*/
function array2xml($data,$tag = '')
{
$xml = '';
foreach($data as $key => $value)
{
if(is_numeric($key))
{
if(is_array($value))
{
$xml .= "<$tag>";
$xml .= array2xml($value);
$xml .="</$tag>";
}
else
{
$xml .= "<$tag>$value</$tag>";
}
}
else
{
if(is_array($value))
{
$keys = array_keys($value);
if(is_numeric($keys[0]))
{
$xml .=array2xml($value,$key);
}
else
{
$xml .= "<$key>";
$xml .=array2xml($value);
$xml .= "</$key>";
}
}
else
{
$xml .= "<$key>$value</$key>";
}
}
}
return $xml;
}
}
xml2array
/**
*
* 将简单的xml转化成关联数组
* @param string $xmlString xml字符串
* @example
* <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RTXConferenceReqDTO>
<conferenceTitle>IT交流会</conferenceTitle>
<startTime>2011-12-19 12:00:00</startTime>
<rtxAccount>andy1111111</rtxAccount>
<ipAddr>192.168.1.56</ipAddr>
<duration>120</duration>
<conferenceType>1</conferenceType>
<invitees>
<invitee>
<rtxAccount>被邀请人1的RTX账号</rtxAccount>
<tel>被邀请人1电话号码</tel>
</invitee>
<invitee>
<rtxAccount>被邀请人2的RTX账号</rtxAccount>
<tel>被邀请人2电话号码</tel>
</invitee>
</invitees>
</RTXConferenceReqDTO>
转化之后的关联数组:
Array
(
[conferenceTitle] => IT交流会
[startTime] => 2011-12-19 12:00:00
[rtxAccount] => andy1111111
[ipAddr] => 192.168.1.56
[duration] => 120
[conferenceType] => 1
[invitees] => Array
(
[invitee] => Array
(
[0] => Array
(
[rtxAccount] => 被邀请人1的RTX账号
[tel] => 被邀请人1电话号码
)
[1] => Array
(
[rtxAccount] => 被邀请人2的RTX账号
[tel] => 被邀请人2电话号码
)
)
)
)
*/
function xml2array($xmlString = '')
{
$targetArray = array();
$xmlObject = simplexml_load_string($xmlString);
$mixArray = (array)$xmlObject;
foreach($mixArray as $key => $value)
{
if(is_string($value))
{
$targetArray[$key] = $value;
}
if(is_object($value))
{
$targetArray[$key] = xml2array($value->asXML());
}
if(is_array($value))
{
foreach($value as $zkey => $zvalue)
{
if(is_numeric($zkey))
{
$targetArray[$key][] = xml2array($zvalue->asXML());
}
if(is_string($zkey))
{
$targetArray[$key][$zkey] = xml2array($zvalue->asXML());
}
}
}
}
return $targetArray;
}
array2xml xml2array的更多相关文章
- PHP 优秀资源汇集(照搬)
文章目录 原文地址: https://shockerli.net/post/php-awesome/ GitHub: https://github.com/shockerli/php-awesome ...
- 使用 Array2XML把数组转成XML格式,有相同的节点
最近开发一个项目,需要调用第三方的接口,第三方提供的数据是xml,我直接使用Array2XML把php数组转成XML格式. XML格式如: <root> <body> < ...
- ***CodeIgniter集成微信支付(转)
微信支付Native扫码支付模式二之CodeIgniter集成篇 http://www.cnblogs.com/24la/p/wxpay-native-qrcode-codeigniter.html ...
- 微信支付Native扫码支付模式二之CodeIgniter集成篇
CI:3.0.5 微信支付API类库来自:https://github.com/zhangv/wechat-pay 请先看一眼官方场景及支付时序图:https://pay.weixin.qq.com/ ...
- php xml 互相转换
正好昨天才做过类似的需求……几行代码就可以搞定. 如果你使用 curl 获取的 xml data$xml = simplexml_load_string($data);$data['tk'] = js ...
- 微信小程序实现支付功能
小程序支付,没有封装支付代码:直接上一段可用的流程代码吧:微信小程序支付官网文档有详细的说明,这里我就不再赘述啦:客户端js: wx.request({ url:'https://www.xxxx.c ...
- ThinkPHP 5 整合支付宝微信支付(支付宝H5,微信H5、APP支付、公众号支付)
因项目没有PC站所以没有写电脑网站支付. Pay.php支付控制器 <?php // +----------------------------------------------------- ...
- 将xml转换为PHP数组
这里提供一个类来将XML转换为PHP数组,下面是类的代码 <?php/** * XML2Array: A class to convert XML to array in PHP * It re ...
- php 数组转xml 数组转json xml转数组 json转数组
array->xml <?php function array2xml($array, $tag) { function ia2xml($array) { $xml="" ...
随机推荐
- WPF 自定义命令 以及 命令的启用与禁用
自定义命令: 在WPF中有5个命令类(ApplicationCommands.NavigationCommands.EditingCommands.ComponentCommands 以及 M ...
- 使用select多选标签笔记
之前一直用checkbox做多选,其实 select也可以多选,只要多给一个属性即可.标签属性 http://www.w3school.com.cn/tags/att_select_multiple. ...
- TestNG入门教程
阅读目录 TestNG介绍 在Eclipse中在线安装TestNG 在Eclipse中离线安装Testng TestNG最简单的测试 TestNG的基本注解 TestNG中如何执行测试 使用testt ...
- /etc/services
/etc/services文件是记录网络服务名和它们对应使用的端口号及协议,很多的系统程序要使用这个文件.一般情况下,不要修改该文件的内容,否则可能会造成端口冲突 常见的服务如下,各个字段分别表示:s ...
- android 动画学习系列(一)
老师今天开始讲android的游戏,讲到了view和surfaceview 下面是view的演示部分 private Paint paint; //游戏里很少用double,都是用long priva ...
- sql数据库中如何根据身份证号判断性别
身份证号有15位和18位的..在sql中该如何判断? I_sex ,) ,) then '男' else '女' END
- java高级---->Thread之Exchanger的使用
Exchanger可以在两个线程之间交换数据,只能是2个线程,他不支持更多的线程之间互换数据.今天我们就通过实例来学习一下Exchanger的用法. Exchanger的简单实例 Exchanger是 ...
- 详解Javascript中prototype属性
转自:https://www.jb51.net/article/91826.htm 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Jav ...
- 有限制的最短路spfa+优先队列
poj1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10751 Accepted: 3952 De ...
- sqlserver 2012隐藏查询结果窗口
1, 08r2的时候都是可以用 ctrl+r隐藏的 到了2012就不行了 方法 . 2, 快捷键的设置