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="" ...
随机推荐
- java中Date的使用情况
在开发中常使用情况. 1.将String转为date 例如"201604131630" //设置日期格式 public SimpleDateFormat sdf = new Si ...
- cocos2d-x游戏引擎核心之十一——并发编程(消息通知中心)
[续] cocos2d-x游戏引擎核心之八——多线程 这里介绍cocos2d-x的一种消息/数据传递方式,内置的观察者模式,也称消息通知中心,CCNotificationCenter. 虽然引擎没有为 ...
- 64位ubuntu下用code::blocks IDE配置opengl开发环境
http://jingyan.baidu.com/article/c74d60007d104f0f6b595d6d.html 样例程序: #include <GL/glut.h> #inc ...
- JSP自定义标签rtexprvalue属性
rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.(比如EL表达式或OGNL表达式). 当在<attribute>标 ...
- 《转》python学习(9)字典
转自 http://www.cnblogs.com/BeginMan/p/3156960.html 一.映射类型 我理解中的映射类型是:键值对的关系,键(key)映射值(value),且它们是一对多的 ...
- 【linux】安装rar,并解压被压缩成多个rar的文件
rar 官网:http://www.rarsoft.com/download.htm 选择 RAR for linux (注意你的系统是32位还是64位) 1 安装命令: $ cd /roo ...
- android基础---->AIDL服务的使用
AIDL和其他的IDL类似,它允许你定义程序接口,以便客户端与服务器端通过IPC机制交互.在android上面,一个进程一般不能访问另外进程的内存.因此,Android平台将这些跨进程访问的对象分解成 ...
- Docker源码分析(三):Docker Daemon启动
1 前言 Docker诞生以来,便引领了轻量级虚拟化容器领域的技术热潮.在这一潮流下,Google.IBM.Redhat等业界翘楚纷纷加入Docker阵营.虽然目前Docker仍然主要基于Linux平 ...
- Servlet 工程 web.xml 中的 servlet 和 servlet-mapping 标签 《转载》
摘录某个工程的 web.xml 文件片段: 访问顺序为1—>2—>3—>4,其中2和3的值必须相同. url-pattern 标签中的值是要在浏览器地址栏中输入的 url,可以自己命 ...
- 关于PreparedStatement.addBatch()方法 (转)
Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接,(打电话拨号 ) Connec ...