php json_encode() 函数格式化数据时会根据不同的数组类型格式化不同类型的json数据
索引数组时
<?...php
$arr = [1,2,3,4,5];
print_r(json_encode($arr));
//输出 [1,2,3,4,5]
关联数组时
<?...php
$arr = [];
print_r(json_encode($arr,JSON_FORCE_OBJECT));
//输出 {}
但是这种存在一个问题,它会把所有的数据都以对象的方式返回,包括索引数组...php
$arr = [1,2,3,4,5,6];
print_r(json_encode($arr,JSON_FORCE_OBJECT));
//输出 {"0":1,"1":2,"2":3,"3":...php
$arr = new ArrayObject();
print_r(json_encode($arr));
//输出 {}
到这里我们可以随意的控制返回的json数据中数据的类型啦!!!!