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的更多相关文章

  1. PHP 优秀资源汇集(照搬)

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

  2. 使用 Array2XML把数组转成XML格式,有相同的节点

    最近开发一个项目,需要调用第三方的接口,第三方提供的数据是xml,我直接使用Array2XML把php数组转成XML格式. XML格式如: <root> <body> < ...

  3. ***CodeIgniter集成微信支付(转)

    微信支付Native扫码支付模式二之CodeIgniter集成篇  http://www.cnblogs.com/24la/p/wxpay-native-qrcode-codeigniter.html ...

  4. 微信支付Native扫码支付模式二之CodeIgniter集成篇

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

  5. php xml 互相转换

    正好昨天才做过类似的需求……几行代码就可以搞定. 如果你使用 curl 获取的 xml data$xml = simplexml_load_string($data);$data['tk'] = js ...

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

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

  7. ThinkPHP 5 整合支付宝微信支付(支付宝H5,微信H5、APP支付、公众号支付)

    因项目没有PC站所以没有写电脑网站支付. Pay.php支付控制器 <?php // +----------------------------------------------------- ...

  8. 将xml转换为PHP数组

    这里提供一个类来将XML转换为PHP数组,下面是类的代码 <?php/** * XML2Array: A class to convert XML to array in PHP * It re ...

  9. php 数组转xml 数组转json xml转数组 json转数组

    array->xml <?php function array2xml($array, $tag) { function ia2xml($array) { $xml="" ...

随机推荐

  1. oracle中REF Cursor用法

    from:http://www.111cn.net/database/Oracle/42873.htm 1,什么是 REF游标 ? 动态关联结果集的临时对象.即在运行的时候动态决定执行查询. 2,RE ...

  2. org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException)

    在运行hadoop的程序时,向hdfs中写文件时候,抛出异常信息如下: Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hado ...

  3. 【RF库Collections测试】Get Slice From List

    Name:Get Slice From ListSource:Collections <test library>Arguments:[ list_ | start=0 | end=Non ...

  4. 多线程的学习与GDI的学习

    今天又是一周的周五,感觉过的好快,我感觉不是期盼周末的到来,而是感觉现在已经习惯啦每天坐在这里敲代码,其实我是一个性格非常开朗的女生,现在不知道为什么感觉默默地坐在自己位置上面敲代码成为了我习以为常的 ...

  5. Discuz 模板标签说明

    Discuz 模板标签说明 Discuz! 的模板采用近似 PHP 表达式的语法,基本都是可识别的HTML,但涉及到变量和动态内容时,基本形式下: <!-{ 代码内容 }-> 逻辑元素包围 ...

  6. JSP自定义标签rtexprvalue属性

    rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.(比如EL表达式或OGNL表达式). 当在<attribute>标 ...

  7. Apktool源码解析——第二篇

    上一篇讲到ApkDecoder这个类,大部分调用到还是Androlib类,而且上次发现brutall的代码竟然不是最新的,遂去找iBotP.的代码了. 今天来看Androlib的代码: private ...

  8. dialog里面数据更新问题

    在实际开发中经常会用到showDialog(int id)的方法来展示一个对话框,但是会遇到一个Dialog展示之后下次再show的时候对话框的界面还是上次展示的那个,而不是我们想象的界面.很多时候我 ...

  9. [数据库系列之MySQL]Mysql优化笔记

    大型网站提速之MySql优化 数据库优化包括的方面 数据库优化是一个综合性的技术,并不是通过某一种方式让数据库效率提高很多,而是通过多方面的提高,从而使得数据库提高很多. 主要包括: 1.表的设计合理 ...

  10. 【Android N 7.1.1】 锁屏之上显示Toast

    package com.android.systemuirom.keyguard; import android.content.Context; import android.view.Gravit ...