相关疑难解决方法(0)

如何将PHP数组编码为JSON数组,而不是对象?

我正在尝试json_encode从Zend_DB查询返回的数组.

var_dump给出:(手动添加0成员不会改变图片.)

array(3) {
  [1]=>
  array(3) {
    ["comment_id"]=>
    string(1) "1"
    ["erasable"]=>
    string(1) "1"
    ["comment"]=>
    string(6) "test 1"
  }
  [2]=>
  array(3) {
    ["comment_id"]=>
    string(1) "2"
    ["erasable"]=>
    string(1) "1"
    ["comment"]=>
    string(6) "test 1"
  }
  [3]=>
  array(3) {
    ["comment_id"]=>
    string(1) "3"
    ["erasable"]=>
    string(1) "1"
    ["comment"]=>
    string(6) "jhghjg"
  }
}
Run Code Online (Sandbox Code Playgroud)

编码的字符串如下所示:

{"1":{"comment_id":"1","erasable":"1","comment":"test 1"},
 "2":{"comment_id":"2","erasable":"1","comment":"test 1"},
 "3":{"comment_id":"3","erasable":"1","comment":"jhghjg"}}
Run Code Online (Sandbox Code Playgroud)

我需要的是:

[{"comment_id":"1","erasable":"1","comment":"test 1"},
{"comment_id":"2","erasable":"1","comment":"test 1"},
{"comment_id":"3","erasable":"1","comment":"jhghjg"}]
Run Code Online (Sandbox Code Playgroud)

这是php.ini/json_encode文档所说的应该是什么样子.

php json zend-framework

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

json ×1

php ×1

zend-framework ×1