Home >Backend Development >PHP Tutorial >php怎么将数组数组转化为json格式的数据

php怎么将数组数组转化为json格式的数据

WBOY
WBOYOriginal
2016-06-23 13:57:111165browse

例如
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);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn