<bdo id='nXt5r'></bdo><ul id='nXt5r'></ul>
  • <tfoot id='nXt5r'></tfoot>

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

      1. <small id='nXt5r'></small><noframes id='nXt5r'>

      2. <legend id='nXt5r'><style id='nXt5r'><dir id='nXt5r'><q id='nXt5r'></q></dir></style></legend>

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

        时间:2023-05-31

            <legend id='bMllq'><style id='bMllq'><dir id='bMllq'><q id='bMllq'></q></dir></style></legend>
              <bdo id='bMllq'></bdo><ul id='bMllq'></ul>

                  <tbody id='bMllq'></tbody>
              1. <small id='bMllq'></small><noframes id='bMllq'>

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

                  <tfoot id='bMllq'></tfoot>
                  本文介绍了如何将 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 数组,而不是对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文章

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

                      • <bdo id='I3mLO'></bdo><ul id='I3mLO'></ul>
                      <legend id='I3mLO'><style id='I3mLO'><dir id='I3mLO'><q id='I3mLO'></q></dir></style></legend>