php中json_encode转化笔记
2017-03-08 09:52
316 查看
PHP支持两种数组,一种是只保存”值”(value)的索引数组(indexed array),另一种是保存”名值对”(name/value)的关联数组(associative array)。
由于javascript不支持关联数组,所以json_encode()只将索引数组(indexed array)转为数组格式,而将关联数组(associative array)转为对象格式。
比如,现在有一个索引数组
arr=Array(‘one′,‘two′,‘three′); echojsonencode(arr);
结果为:
[“one”,”two”,”three”]
如果将它改为关联数组:
arr=Array(‘1′=>′one′,‘2′=>′two′,‘3′=>′three′); echojsonencode(arr);
结果就变了:
{“1”:”one”,”2”:”two”,”3”:”three”}
如果获得下面的格式:
当前只会用嵌套数组方法
$menu=array(array(id=>’index’,homePage=>’main’,menu=>array(array(text=>’测试’,items=>array(array(id=>’main’,text=>’首页’,href=>’Index/index’))))));
由于javascript不支持关联数组,所以json_encode()只将索引数组(indexed array)转为数组格式,而将关联数组(associative array)转为对象格式。
比如,现在有一个索引数组
arr=Array(‘one′,‘two′,‘three′); echojsonencode(arr);
结果为:
[“one”,”two”,”three”]
如果将它改为关联数组:
arr=Array(‘1′=>′one′,‘2′=>′two′,‘3′=>′three′); echojsonencode(arr);
结果就变了:
{“1”:”one”,”2”:”two”,”3”:”three”}
如果获得下面的格式:
var config=[{ "id": "index", "homePage": "main", "menu": [{ "text": "测试", "items": [{ "id": "main", "text": "首页", "href": "Index/index" }] },{ "text": "测试2", "items": [{ "id": "main2", "text": "首页", "href": "Index/index" }] } ] }];
当前只会用嵌套数组方法
$menu=array(array(id=>’index’,homePage=>’main’,menu=>array(array(text=>’测试’,items=>array(array(id=>’main’,text=>’首页’,href=>’Index/index’))))));
相关文章推荐
- php 序列化储存和转化 json_encode() json_decode($q,true)
- php笔记(11)——json_encode后中文编码问题
- php中对于json_decode()和json_encode()的使用方法笔记
- jquery的ajax处理php返回数组,及json_encode中文问题
- php json_encode 后js处理时对中文和韩文的问题
- 将PHP数组赋值给JS通过php系统函数json_encode()来实现
- PHP json_encode() 函数介绍
- php json_encode 问题解决小记
- PHP json_encode() 函数介绍
- php中json_encode中文编码问题
- PHP json_encode() 函数介绍
- php+json+ajax解决中文乱码笔记
- php网站服务器没有json包(json_encode和json_decode)方法解决
- php中json_encode中文编码问题
- PHP学习散记_编码(json_encode 中文不显示)
- php json_encode奇怪问题说明
- PHP5.1 下启用json_encode方法
- [PHP] 从 json_encode 到 ajax_encode
- 将PHP数组或对象传递给javascript 交互 传值 json_encode
- php json_encode怪问题