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;

}

转载于:https://www.cnblogs.com/honeynm/p/5279807.html

array2xml xml2array相关推荐

  1. PHP 优秀资源汇集(转)

    文章目录 原文地址: https://shockerli.net/post/php-awesome/ GitHub: https://github.com/shockerli/php-awesome ...

  2. PHP XML操作类 xml2array -- 含节点属性

    1. 单向xml2array函数 function xml2array($contents, $get_attributes=1, $priority = 'tag') {if(!$contents) ...

  3. php里Array2xml

    <?php class Array2xml {     var $xml;     function array2xml($array,$encoding='utf-8') {          ...

  4. php xml 互相转换

    php xml 互相转换 正好昨天才做过类似的需求--几行代码就可以搞定. 如果你使用 curl 获取的 xml data $xml = simplexml_load_string($data); $ ...

  5. 微信小程序实现支付功能

    小程序支付,没有封装支付代码:直接上一段可用的流程代码吧: 微信小程序支付官网文档有详细的说明,这里我就不再赘述啦: 客户端js: wx.request({ url:'https://www.xxxx ...

  6. php微信支付分取消订单,PHP实现微信支付和退款

    这次给大家带来PHP实现微信支付和退款,PHP实现微信支付和退款的注意事项有哪些,下面就是实战案例,一起来看一下. 之前有写过几篇文章将微信支付和退款: 1.PHP实现微信支付(jsapi支付)流程 ...

  7. 教程08-微擎系统内置所有函数大全

    微擎系统内置了很多共函数.HTTP请求函数.文件和数据操作函数. 1. 系统公共函数 系统全局公共函数全部位于 framework/function/global.func.php 文件内. 1.1  ...

  8. ThinkPHP 微信支付及退款

    目录 微信支付 微信退款 1.以下代码修改完自己的 2.appid 3.商户号 4.商户密钥 微信支付 //微信支付public function index(){//接收用户下单信息$data = ...

  9. php CI 微信支付扩展 微信扫码支付 jssdk 支付 退款

    微信支付API类库来自:https://github.com/zhangv/wechat-pay 请先看一眼官方场景及支付时序图:https://pay.weixin.qq.com/wiki/doc/ ...

最新文章

  1. CIKM投稿数量1700篇,图神经网络成热门方向,最佳论文纷纷进行图研究
  2. .net简单算法实现无限级分类(一)
  3. 面试题解析:1 Java中switch语句可以作用在enum上的测试
  4. java 事件监听器 执行类_事件及事件监听器类 java
  5. 程序员面试金典——5.8像素设定
  6. js 数字格式化,只能输入正负整数,小数
  7. xml简单理解,xml增删改操作,仅作笔记,不作为学习借鉴
  8. 原生JS与其他JS 区别
  9. 软件程序设计中的N-S图、PAD图、程序流程图、E-R图
  10. 2021年危险化学品经营单位安全管理人员新版试题及危险化学品经营单位安全管理人员模拟考试系统
  11. 解决AndroidStudio报错问题:Missing essential plugin
  12. sort函数(c语言排序)
  13. 计算机突然断电或死机 重启后,电脑重启死机故障排除
  14. adb server is out of date 最新解决方案
  15. “background-image:url(data:image”data类型的Url格式简介
  16. 物流人必备宝藏软件安利——Microcity
  17. ROS msg 文件修改 报错
  18. 于Mozilla平台的扩展开发
  19. (附源码)springboot菠萝大学课室预约系统分析与设 毕业设计641656
  20. 技巧篇:常用的python代码汇总

热门文章

  1. vue中axios的基本使用,天气查询案例
  2. python集成开发环境运行快捷键_Python初学者选择集成开发环境必看 python开发
  3. Socket API: setsockopt(), recvmsg(), sendmsg()函数
  4. GDAL读写矢量文件——Java
  5. SQL那些事儿(十二)--DATASET 与 DATAREADER区别
  6. Starling常见问题解决办法
  7. jQuery Mobile 高级设计模板
  8. coco数据集大小分类_【数据集】LVIS:大规模细粒度词汇级标记数据集 ,出自FAIR ,连披萨里的菠萝粒都能完整标注...
  9. android adb模拟点击,Android adb 模拟滑动 按键 点击事件(示例代码)
  10. ros多机通讯的办法