博客
关于我
array与xml转换实现(转)
阅读量:411 次
发布时间:2019-03-06

本文共 1645 字,大约阅读时间需要 5 分钟。

'; $xml .= "<{$root}>"; $xml .= array_to_xml($data); $xml .= "
"; return $xml; } function xml_decode($xml, $root = 'so') { $search = '/<(' . $root . ')>(.*)<\/\s*?\\1\s*?>/s'; $array = array(); if(preg_match($search, $xml, $matches)){ $array = xml_to_array($matches[2]); } return $array; } function array_to_xml($array) { if(is_object($array)){ $array = get_object_vars($array); } $xml = ''; foreach($array as $key => $value){ $_tag = $key; $_id = null; if(is_numeric($key)){ $_tag = 'item'; $_id = ' id="' . $key . '"'; } $xml .= "<{$_tag}{$_id}>"; $xml .= (is_array($value) || is_object($value)) ? array_to_xml($value) : htmlentities($value); $xml .= "
"; } return $xml; } function xml_to_array($xml) { $search = '/<(\w+)\s*?(?:[^\/>]*)\s*(?:\/>|>(.*?)<\/\s*?\\1\s*?>)/s'; $array = array (); if(preg_match_all($search, $xml, $matches)){ foreach ($matches[1] as $i => $key) { $value = $matches[2][$i]; if(preg_match_all($search, $value, $_matches)){ $array[$key] = xml_to_array($value); }else{ if('ITEM' == strtoupper($key)){ $array[] = html_entity_decode($value); }else{ $array[$key] = html_entity_decode($value); } } } } return $array; }

 

转载地址:http://iblkz.baihongyu.com/

你可能感兴趣的文章
MySQL配置信息解读(my.cnf)
查看>>
Mysql配置文件my.ini详解
查看>>
MySQL配置文件深度解析:10个关键参数及优化技巧---强烈要求的福利来咯。
查看>>
Mysql配置表名忽略大小写(SpringBoot连接表时提示不存在,实际是存在的)
查看>>
mysql配置读写分离并在若依框架使用读写分离
查看>>
MySQL里为什么会建议不要使用SELECT *?
查看>>
MySQL里的那些日志们
查看>>
MySQL锁
查看>>
MySQL锁与脏读、不可重复读、幻读详解
查看>>
MySQL锁机制
查看>>
mysql锁机制,主从复制
查看>>
Mysql锁机制,行锁表锁
查看>>
MySQL锁表问题排查
查看>>
Mysql锁(1):锁概述和全局锁的介绍
查看>>
Mysql锁(2):表级锁
查看>>
MySQL锁,锁的到底是什么?
查看>>
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
查看>>
Mysql错误2003 -Can't connect toMySQL server on 'localhost'(10061)解决办法
查看>>
MySQL错误提示mysql Statement violates GTID consistency
查看>>
mysql错误:This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de
查看>>