惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
博客园 - 【当耐特】
H
Heimdal Security Blog
腾讯CDC
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threatpost
月光博客
月光博客
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
D
DataBreaches.Net
O
OpenAI News
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
小众软件
小众软件
V
Vulnerabilities – Threatpost
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
美团技术团队
P
Privacy International News Feed

博客园 - Stym--闫生

最简单的silverlight的DataGrid行双击事件添加 == 在cmd命令下imp oracle dmp文件 flex TabNavigator 切换之前给出提示 - Stym--闫生 vb.net连接oracle和php连接oracle - Stym--闫生 - 博客园 数据库名,全局数据库名,SID 创建同义词和赋权限 linux下创建硬连接 flex 的as文件引用,包引用 - Stym--闫生 - 博客园 flex分页控件 - Stym--闫生 - 博客园 数据处理:等待条 php使用ajax技术 javascript拖住布局demo flex + Amfphp + mysql +IIS 5.1t 用sql查看表结构 c#生成验证图片 C#使用sql语句读取excel文件数据 svn切换用户,报“501 Method Not Implemented”错误 div滤镜结合ajax,实现登录
把数组转化成一个xml字符串--php - Stym--闫生 - 博客园
Stym--闫生 · 2008-11-27 · via 博客园 - Stym--闫生

一:代码:

<?php
 
//************************test***************************
$vararray   =   array(  
                          "name"     =>   array("alen", "andy", "tom"),
                          "cuntry"   => array("china","japan","english"),
                          "number"   => array(1,2,3, array(11,22,33))
                      );  
$pp = toXml($vararray,"gb2312");
echo $pp;

/**
*把给定的数组转化成一个xml字符串
*
* @author [yans@tks-it.com.cn]
* @param  $arrays  :要转换的数组
* @param  $encoding:xml文件的编码
* @return string
*/
function toXml($arrays,$encoding='')
{
 // 数组检测
 if(!is_array($arrays))
 {
  echo "<font color='red'>$arrays is not a array!</font>";
  return false;
 }
 $xmlstr = '';
 // xml文件编码设置
 if($encoding=='')
 {
  $xmlstr  .='<?xml version="1.0" encoding="utf-8" ?>';
 }
 else
 {
  $xmlstr  .='<?xml version="1.0" encoding="';
  $xmlstr  .=$encoding;
  $xmlstr  .='" ?>';
 }
 $xmlstr .='<root>';
 // 调用遍历数组函数
 $xmlstr .= traversal($arrays);
 $xmlstr .='</root>';
 return $xmlstr;
}

/**
*遍历数组,成一个xml片段字符串
*
* @author [yans@tks-it.com.cn]
* @param  $arr   :要遍历的数组
* @return string
*/  
function traversal($arr)
{
  $arrString = '';
  while($newArr = each($arr))
  {
     $arrString .= "<$newArr[0]>";
     if(is_array($newArr[1]))
     {
        $tempString = traversal($newArr[1]);
        $arrString .= $tempString;
     }
     else
     {
        $arrString .= "$newArr[1]";
     }
    $arrString .= "</$newArr[0]>";
  }
  return $arrString;
}
?>

二:执行的结果:(结果已经手动调整,便于观察)

<?xml version="1.0" encoding="gb2312" ?>
<root>
<name>
<0>alen</0>
<1>andy</1>
<2>tom</2>
</name>

<cuntry>
<0>china</0>
<1>japan</1>
<2>english</2>
</cuntry>

<number>
<0>1</0>
<1>2</1>
<2>3</2>
<3>
 <0>11</0>
 <1>22</1>
 <2>33</2>
</3>
</number>

</root>

三:备注:

注意:xml的标签是以下划线和字母开头的,且不能又xml字样的字符串!