PHP XML To Array将XML转换为数组
// Xml 转 数组, 包括根键,忽略空元素和属性,尚有重大错误
function xml_to_array( $xml )
{
$reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/";
if(preg_match_all($reg, $xml, $matches))
{
$count = count($matches[0]);
$arr = array();
for($i = 0; $i < $count; $i++)
{
$key= $matches[1][$i];
$val = xml_to_array( $matches[2][$i] ); // 递归
if(array_key_exists($key, $arr))
{
if(is_array($arr[$key]))
{
if(!array_key_exists(0,$arr[$key]))
{
$arr[$key] = array($arr[$key]);
}
}else{
$arr[$key] = array($arr[$key]);
}
$arr[$key][] = $val;
}else{
$arr[$key] = $val;
}
}
return $arr;
}else{
return $xml;
}
}
// Xml 转 数组, 不包括根键
function xmltoarray( $xml )
{
$arr = xml_to_array($xml);
$key = array_keys($arr);
return $arr[$key[0]];
}
// 类似 XPATH 的数组选择器
function xml_array_select( $arr, $arrpath )
{
$arrpath = trim( $arrpath, '/' );
if(!$arrpath) return $arr;
$self = 'xml_array_select'; $pos = strpos( $arrpath, '/' );
$pos = $pos ? $pos : strlen($arrpath);
$curpath = substr($arrpath, 0, $pos);
$next = substr($arrpath, $pos); if(preg_match("/\\[(\\d+)\\]$/",$curpath,$predicate))
{
$curpath = substr($curpath, 0, strpos($curpath,"[{$predicate[1]}]"));
$result = $arr[$curpath][$predicate[1]];
}else $result = $arr[$curpath]; if( is_array($arr) && !array_key_exists($curpath, $arr) )
{
die( 'key is not exists:' . $curpath );
} return $self($result, $next);
}
// 如果输入的数组是全数字键,则将元素值依次传输到 $callback, 否则将自身传输给$callback
function xml_array_each( $arr, $callback )
{
if(func_num_args()<2) die('parameters error');
if(!is_array($arr)) die('parameter 1 shuld be an array!');
if(!is_callable($callback)) die('parameter 2 shuld be an function!');
$keys = array_keys($arr);
$isok = true;
foreach( $keys as $key ) {if(!is_int($key)) {$isok = false; break;}}
if($isok)
foreach( $arr as $val ) $result[] = $callback($val);
else
$result[] = $callback( $arr );
return $result;
}
/**
* 最简单的XML转数组
* @param string $xmlstring XML字符串
* @return array XML数组
*/
function simplest_xml_to_array($xmlstring) {
return json_decode(json_encode((array) simplexml_load_string($xmlstring)), true);
}
PHP XML To Array将XML转换为数组的更多相关文章
- 将xml转为array 输出xml字符
//将xml转为array private function fromXml($xml){ // 禁止引用外部xml实体 libxml_disable_entity_loader(true); ret ...
- 一个简单xml数据转换为数组的方法
本人用easywechat做微信回复图文,从数据库中拿到的数据直接是xml拼好的数据,但是框架只有自带的获取xml格式的语句,所有需要将xml数据中所需要的数据拿出来用来拼接. 搜了好多资料说的都很麻 ...
- php xml转数组,数组转xml,array转xml,xml转array
//数组转XML function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) ...
- 微信开发所需要的的方法(签名认证、数组转字符串方法、将xml字符串转换为数组、发送xml请求方法)
//将xml字符串转换为数组 public function xmlToArray($xml){ $array_data = json_decode(json_encode(simplexml_loa ...
- PHP将XML数据转换为数组
<?php $s=join(,file('httpapi.elong.comxmlv2.0hotelcn0132701501.xml')); $result = xml_to_array($s) ...
- php xml格式对象 返回->对应格式数组
/* * $objXml xml格式对象 * 返回 : 对应格式数组 */ public function XmlString2Arr($xml) { ...
- PHP:微信小程序调用【统一下单】【微信支付】【支付回调】API;XML转Array,Array转XML方法(通用)
1.微信公众号.微信小程序开发过程中,第三方服务器与微信服务器数据交互,需要进行数据转换,必须用到这两个函数: 分别是xml_to_array.array_to_xml ; /** * 输出xml字符 ...
- php xml转array的方法
php xml转array的方法 <pre><?php $responseXml='<xml><appid>12</appid></xml& ...
- xml格式的数据转化成数组
将得到的xml格式的数据转化成数组 <?php //构造xml $url = "http://api.map.baidu.com/telematics/v3/weather?locat ...
随机推荐
- 03《UML大战需求分析》之三
学习了活动图之后,我又学习了流程分析工具之二的状态机图.看上去状态机图和活动图很类似,我也很容易从活动图的角度来理解状态机图.但是学习之后,发现两种图是两种完全不同的分析角度.活动图在流程分析时是玩你 ...
- 手写一个promise
Promise A+ 规范:https://promisesaplus.com/ 注:以下代码没有通过 promises-aplus-tests 的全部测试,但基本功能还是全的( 测试结果: 864 ...
- [USACO18FEB] Snow Boots G (离线+并查集)
题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...
- B-Tree概念
记录下学习B-Tree: concept:(m-阶) 1. 根节点 孩子数 ( 2 <= N <= m) 根节点key数([m/2] - 1 <= n <= m -1) 2 ...
- Shiro:授权的相关实现
Shiro:授权的相关实现 一.使用Shiro过滤器实现授权 设置好授权拦截跳转的请求地址 /** * 创建ShiroFilterFactoryBean */ @Bean public ShiroFi ...
- [terry笔记]对人员列表文件进行数据库操作
原文件(数据已经脱敏): staff_id,name,age,phone,dept,enroll_date1,姬建明,25,152015410,运维,2013-11-012,刘海龙,26,186184 ...
- 洛谷 2921 记忆化搜索 tarjan 基环外向树
洛谷 2921 记忆化搜索 tarjan 传送门 (https://www.luogu.org/problem/show?pid=2921) 做这题的经历有点玄学,,起因是某个random题的同学突然 ...
- PatentTips - Object-oriented processor architecture and operating method
BACKGROUND OF THE INVENTION The present invention relates to processors and computer systems. More s ...
- iOS-UIImage imageWithContentsOfFile 和 imageName 对照
1.imageWithContentsOfFile NSString *imagePath = [NSString stringWithFormat:@"%@/%@",[[NSBu ...
- Node.js 博客实例(一)简单博客
原教程 https://github.com/nswbmw/N-blog/wiki/_pages的第一章.因为版本号等的原因,在原教程基础上稍加修改就可以实现. 环境: win7旗舰版64位 Node ...