您的位置:首页 > 编程语言 > PHP开发

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”}

  

如果获得下面的格式:

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