• <small id='Zh1Mp'></small><noframes id='Zh1Mp'>

      <i id='Zh1Mp'><tr id='Zh1Mp'><dt id='Zh1Mp'><q id='Zh1Mp'><span id='Zh1Mp'><b id='Zh1Mp'><form id='Zh1Mp'><ins id='Zh1Mp'></ins><ul id='Zh1Mp'></ul><sub id='Zh1Mp'></sub></form><legend id='Zh1Mp'></legend><bdo id='Zh1Mp'><pre id='Zh1Mp'><center id='Zh1Mp'></center></pre></bdo></b><th id='Zh1Mp'></th></span></q></dt></tr></i><div id='Zh1Mp'><tfoot id='Zh1Mp'></tfoot><dl id='Zh1Mp'><fieldset id='Zh1Mp'></fieldset></dl></div>
      <legend id='Zh1Mp'><style id='Zh1Mp'><dir id='Zh1Mp'><q id='Zh1Mp'></q></dir></style></legend>
      • <bdo id='Zh1Mp'></bdo><ul id='Zh1Mp'></ul>

      1. <tfoot id='Zh1Mp'></tfoot>

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

        时间:2023-10-03
        <tfoot id='0TdTT'></tfoot>

          • <bdo id='0TdTT'></bdo><ul id='0TdTT'></ul>
              1. <small id='0TdTT'></small><noframes id='0TdTT'>

                  <tbody id='0TdTT'></tbody>
                <legend id='0TdTT'><style id='0TdTT'><dir id='0TdTT'><q id='0TdTT'></q></dir></style></legend>
                • <i id='0TdTT'><tr id='0TdTT'><dt id='0TdTT'><q id='0TdTT'><span id='0TdTT'><b id='0TdTT'><form id='0TdTT'><ins id='0TdTT'></ins><ul id='0TdTT'></ul><sub id='0TdTT'></sub></form><legend id='0TdTT'></legend><bdo id='0TdTT'><pre id='0TdTT'><center id='0TdTT'></center></pre></bdo></b><th id='0TdTT'></th></span></q></dt></tr></i><div id='0TdTT'><tfoot id='0TdTT'></tfoot><dl id='0TdTT'><fieldset id='0TdTT'></fieldset></dl></div>
                  本文介绍了如何将 PHP 数组编码为 JSON 数组,而不是对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I am trying to json_encode an array which is returned from a Zend_DB query.

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

                  var_dump gives: (Manually adding 0 member does not change the picture.)

                  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"
                    }
                  }
                  

                  编码后的字符串如下所示:

                  The encoded string looks like:

                  {"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"}}
                  

                  我需要的是:

                  [{"comment_id":"1","erasable":"1","comment":"test 1"},
                  {"comment_id":"2","erasable":"1","comment":"test 1"},
                  {"comment_id":"3","erasable":"1","comment":"jhghjg"}]
                  

                  php.ini/json_encode 文档说它应该是什么样子.

                  Which is what the php.ini/json_encode documentation says it should look like.

                  推荐答案

                  您如何设置初始数组?

                  如果你这样设置:

                  array(
                   "1" => array(...),
                   "2" => array(...),
                  );
                  

                  那么你没有一个带有数字索引的数组,而是字符串,在 JS 世界中它被转换为一个对象.如果您没有设置严格的顺序(即从 0 而不是 1 开始),也会发生这种情况.

                  then you don't have an array with numeric indexes but strings, and that's converted to an object in JS world. This can happen also if you don't set a strict order (i.e. starting at 0 instead of 1).

                  然而,这是在黑暗中拍摄,因为我看不到您的原始代码:首先尝试在不使用键的情况下设置您的数组:

                  This is a shot in the dark, however, because I can't see your original code: try setting your array without using keys at all in the first place:

                  array(
                   array(...),
                   array(...),
                  );
                  

                  这篇关于如何将 PHP 数组编码为 JSON 数组,而不是对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Zend Framework - 在选择框下拉列表中设置“选定&q 下一篇:Zend_Form:如何检查 2 个字段是否相同

                  相关文章

                  最新文章

                • <legend id='yNfd1'><style id='yNfd1'><dir id='yNfd1'><q id='yNfd1'></q></dir></style></legend>

                    1. <tfoot id='yNfd1'></tfoot>

                    2. <small id='yNfd1'></small><noframes id='yNfd1'>

                        <bdo id='yNfd1'></bdo><ul id='yNfd1'></ul>
                      <i id='yNfd1'><tr id='yNfd1'><dt id='yNfd1'><q id='yNfd1'><span id='yNfd1'><b id='yNfd1'><form id='yNfd1'><ins id='yNfd1'></ins><ul id='yNfd1'></ul><sub id='yNfd1'></sub></form><legend id='yNfd1'></legend><bdo id='yNfd1'><pre id='yNfd1'><center id='yNfd1'></center></pre></bdo></b><th id='yNfd1'></th></span></q></dt></tr></i><div id='yNfd1'><tfoot id='yNfd1'></tfoot><dl id='yNfd1'><fieldset id='yNfd1'></fieldset></dl></div>