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 {...做数组处理时会使默认索引丢失,可以使用array_values()初始化索引】
$attr = array("0"=>"a","1"=>"b","3"=>"c","4"=>"d","5"=>"e");
转换json