php中json_encode转化笔记

来源:互联网 发布:知己知皮下一句是什么 编辑:程序博客网 时间:2024/05/16 08:10

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’))))));

0 0