大家好,又见面了,我是你们的朋友全栈君。
php中数组转json的规则是:当没有指定索引(0~n)时会转换为json数组,而指定了索引会转换为json对象。...PHP的数组在转JSON的时候,如果索引连续,则转成数组。...如果索引不连续,则会转成对象
1、没有指定索引的情况:
$attr = array("a","b","c","d","e");
转换为json:
["a","b","c","d","e"]
2、有指定索引的情况...:
$attr = array("a"=>"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e");
转换为json:
1
2
3
4
5
6
7 {..."a": "a",
"b": "b",
"c": "c",
"d": "d",
"e": "e"
}
3、默认索引,但是索引不连续,也会转成对象【unset() 做数组处理时会使默认索引丢失