Rumah >pembangunan bahagian belakang >tutorial php >php怎么将数组数组转化为json格式的数据
例如
Array
(
[0] => a
[1] => b
[2] => c
[3] => h
[4] => f
)
我直接使用json_encode($a); //$a就是上面使用print_r()打印出的结果
结果显示为["a","b","c","h","f"],而事实上我想要的是{"0":"a","1":"b","2":"c","3":"h","4":"f"},只有这种格式我在ios接收到的数据才不为空
那就自己拼装?
$a = array('a', 'b', 'c', 'd');foreach($a as $k=>$v) $b[] = sprintf('"%s":"%s"', $k, $v);echo '{' . join(',', $b) . '}';{"0":"a","1":"b","2":"c","3":"d"}
已解决,将索引数组转化为对象即可,json_encode((object)$a);