博客
关于我
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执行SQL文件出现【Unknown collation ‘utf8mb4_0900_ai_ci‘】的解决方案
查看>>
Mysql执行update by id的过程
查看>>
mysql执行计划
查看>>
MySQL执行计划 EXPLAIN参数
查看>>
MySQL执行计划【explain】,看这一篇就够啦!
查看>>
Mysql执行计划字段解释
查看>>
mysql执行计划怎么看
查看>>
MySQL执行计划解读
查看>>
mysql执行顺序与索引算法
查看>>
mysql批量update优化_Mysql中,21个写SQL的好习惯,你值得拥有呀
查看>>
mysql批量update操作时出现锁表
查看>>
MYSQL批量UPDATE的两种方式
查看>>
mysql批量修改字段名(列名)
查看>>
MySQL批量插入数据遇到错误1213的解决方法
查看>>
mysql技能梳理
查看>>
MySQL报Got an error reading communication packets错
查看>>
Mysql报错Can‘t create/write to file ‘/tmp/#sql_3a8_0.MYD‘ (Errcode: 28 - No space left on device)
查看>>
MySql报错Deadlock found when trying to get lock; try restarting transaction 的问题解决
查看>>
MySQL报错ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘
查看>>
Mysql报错Packet for query is too large问题解决
查看>>