array->xml

<?php
function array2xml($array, $tag) { function ia2xml($array) {
$xml="";
foreach ($array as $key=>$value) {
if (is_array($value)) {
$xml.="<$key>".ia2xml($value)."</$key>";
} else {
$xml.="<$key>".$value."</$key>";
}
}
return $xml;
} return simplexml_load_string("<$tag>".ia2xml($array)."</$tag>");
} $test['type']='lunch';
$test['time']='12:30';
$test['menu']=array('entree'=>'salad', 'maincourse'=>'steak'); echo array2xml($test,"meal")->asXML();
?>

xml->array

method1(more use):

function xml2phpArray($xml,$arr){
$iter = 0;
foreach($xml->children() as $b){
$a = $b->getName();
if(!$b->children()){
$arr[$a] = trim($b[0]);
}else{
$arr[$a][$iter] = array();
$arr[$a][$iter] = xml2phpArray($b,$arr[$a][$iter]);
$iter++;
}
}
return $arr;
} $xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
<a><c>ccc</c><e>eee</e></a>
</note>
XML; print_r(xml2phpArray(simplexml_load_string ( $xml ),array()));

method2:

function XML2Array ( $xml , $recursive = false )
{
if ( ! $recursive )
{
$array = simplexml_load_string ( $xml ) ;
}
else
{
$array = $xml ;
} $newArray = array () ;
$array = ( array ) $array ;
foreach ( $array as $key => $value )
{
$value = ( array ) $value ;
if ( isset ( $value [ 0 ] ) )
{
$newArray [ $key ] = trim ( $value [ 0 ] ) ;
}
else
{
$newArray [ $key ] = XML2Array ( $value , true ) ;
}
}
return $newArray ;
} $xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
<a><b><c>ccc</c></b><e>eee</e></a>
</note>
XML; print_r(XML2Array($xml));

json->array

json_decode($json,true);//第二个参数为true时 即为array

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

array->json

json_encode 数组-》json

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo json_encode($arr);

php 数组转xml 数组转json xml转数组 json转数组的更多相关文章

  1. PHP XML To Array将XML转换为数组

    // Xml 转 数组, 包括根键,忽略空元素和属性,尚有重大错误 function xml_to_array( $xml ) { $reg = "/<(\\w+)[^>]*?& ...

  2. Python导出Excel为Lua/Json/Xml实例教程(一):初识Python

    Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...

  3. JSON&XML总结

    JSON&XML: JSON----- //英译 Serialization:序列化 perform:执行 segue:继续 IOS5后 NSJSONSerialization解析 解析JSO ...

  4. 计算机程序的思维逻辑 (63) - 实用序列化: JSON/XML/MessagePack

    上节,我们介绍了Java中的标准序列化机制,我们提到,它有一些重要的限制,最重要的是不能跨语言,实践中经常使用一些替代方案,比如XML/JSON/MessagePack. Java SDK中对这些格式 ...

  5. 如何使用Google APIs和Google应用系统集成(7)----在里面JSON兑换XML数据处理,JSON数据包括违规XML数据规范:XML节点名称不支持号码Java解

    笔者电话Google Calendar APIs的GetColors方法,其中(有关详细信息Google Calendar API已经Google API看到我的博文介绍的其余部分,目前,我们只取Go ...

  6. JSON & XML 简析

    转载自:http://my.oschina.net/aofe/blog/269260 JSON: XML: JSON格式说明: HTML & XML 的对比 HTML: XML: HTML5新 ...

  7. Java对象转换成xml对象和Java对象转换成JSON对象

    1.把Java对象转换成JSON对象 apache提供的json-lib小工具,它可以方便的使用Java语言来创建JSON字符串.也可以把JavaBean转换成JSON字符串. json-lib的核心 ...

  8. 分析Json/Xml的解析过程

    json和xml都是用来进行数据的传输和交换的,是目前用来传输数据用的最多的两种技术,下面我们分别来认识一下它们及其解析过程 一.json 1.json简介 JSON是一种基于文本的轻量级数据交换格式 ...

  9. Json&xml分析~

    1.什么是Json? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Prog ...

随机推荐

  1. 瞄一眼LongAdder(jdk11)

    java版本11.0.1,感觉写得太水了,等心情好的时候再重新编辑一下. LongAdder中的核心逻辑主要由java.util.concurrent.atomic.Striped64维护,作为Str ...

  2. 【Visual Studio】The project appears to be under source control, but the associated source control plug-in is not installed on this computer

    [问题描述]用 Visual Studio 2013打开一个项目时,出现下面错误: [问题原因]参考 http://codeverge.com/asp.net.web-forms/the-projec ...

  3. How to fix the gray screen bug in VirtualBox

    If you see a gray screen instead of GNOME when entering the system, simply switch to a virtual conso ...

  4. UML学习倒腾记

    先看到http://www.jianshu.com/p/1256e2643923这篇博客,号称21分钟入门uml,也许是我太笨了吧,一下午也没有完全搞定: 使用过atom编辑器,没有完全运行出来结果. ...

  5. Codeforces Gym101502 A.Very Hard Question

    2017 JUST Programming Contest 3.0 昨天的训练赛,打的好难过,因为被暴打了,写了8题,他们有的写了9题,差了一道dp,博客上写7道题的题解. 因为有一道是套板子过的,并 ...

  6. 100 Most Influential Books According to Stack Overflow

    Please read this blog post to see why this is here. This data was created on 02/13/2012 20:00:00 All ...

  7. cocos2d-x step by step(2) 鼠标事件,键盘事件,等等事件

    各种小控件加载进去了,那么问题来了,这些东西如何接受事件呢? good job,let us find the answer 首先我们去看文档,官方尼玛有好多文档,而且大,全,详细,感觉还是不错的 h ...

  8. iOS -- SKTextureAtlas类

      SKTextureAtlas类 继承自 NSObject 符合 NSCodingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKi ...

  9. 查看java中的线程个数名称

    查看java中的线程个数名称 package com.stono.thread2; import java.lang.management.ManagementFactory; import java ...

  10. c# Dictionary拓展2个key得到1个value

    using System.Collections.Generic; using System.Collections; Dictionary<Tuple<int, int>, int ...