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

php如何将数组数组转化为json格式的数据

WBOY
Release: 2016-06-13 11:57:07
Original
814 people have browsed it

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');<br />foreach($a as $k=>$v) $b[] = sprintf('"%s":"%s"', $k, $v);<br />echo '{' . join(',', $b) . '}';
Copy after login
{"0":"a","1":"b","2":"c","3":"d"}

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template