1

我正在尝试发现什么 php 数组为您提供了这个 json

{
  "sEcho": 67,
  "iTotalRecords": "4075",
  "iTotalDisplayRecords": "4075",
  "aaData": [
    [
      "215",
      "Manaus",
      "BRA",
      "Amazonas",
      "1255049"
    ],
    [
      "216",
      "Belém",
      "BRA",
      "Pará",
      "1186926"
    ],
    [
      "217",
      "Guarulhos",
      "BRA",
      "São Paulo",
      "1095874"
    ],
    [
      "218",
      "Goiânia",
      "BRA",
      "Goiás",
      "1056330"
    ],
    [
      "219",
      "Campinas",
      "BRA",
      "São Paulo",
      "950043"
    ],
    [
      "220",
      "São Gonçalo",
      "BRA",
      "Rio de Janeiro",
      "869254"
    ],
    [
      "221",
      "Nova Iguaçu",
      "BRA",
      "Rio de Janeiro",
      "862225"
    ],
    [
      "222",
      "São Luís",
      "BRA",
      "Maranhão",
      "837588"
    ],
    [
      "223",
      "Maceió",
      "BRA",
      "Alagoas",
      "786288"
    ],
    [
      "224",
      "Duque de Caxias",
      "BRA",
      "Rio de Janeiro",
      "746758"
    ]
  ]
}

我正在尝试这个

$array = array(
    "foo" => "more foo",
    "st" => "bar",
    "ty" => "stuff",
    "multi" => array(
             "array" => "foo",
             "the" => "stuff"
         ),
         array(array(
             "array" => "foo",
             "the" => "stuff"
         )),
         array(array(
             "array" => "foo",
             "the" => "stuff"
         )),
         array(array(
             "array" => "foo",
             "the" => "stuff"
         ))
    );
echo "<pre>";
print_r(json_encode($array));
echo "</pre>";

但我的 json 有一个大括号

{"foo":"more foo","st":"bar","ty":"stuff","multi":{"array":"foo","the":"stuff"},"0 ":[{"array":"foo","the":"stuff"}],"1":[{"array":"foo","the":"stuff"}],"2": [{"array":"foo","the":"stuff"}]}

如何更正我的数组以生成 json?

4

4 回答 4

3

你有太多的深度数组,你不应该指定键。在 PHP 数组中,如果您设置一个字符串键并 JSON 对其进行编码,它将生成一个对象而不是数组。

$array = array(
    "foo" => "more foo",
    "st" => "bar",
    "ty" => "stuff",
    "multi" => array(
        array(
            '215',
            'Manaus',
            'BRA'
        ),
        array(
            '216',
            'Belém',
            'BRA'
        ),
        array(
            '217',
            'Guarulhos',
            'BRA'
        )
    )
);

echo json_encode($array);

输出:

{
    "foo": "more foo",
    "st": "bar",
    "ty": "stuff",
    "multi": [
        [
            "215",
            "Manaus",
            "BRA"
        ],
        [
            "216",
            "Belém",
            "BRA"
        ],
        [
            "217",
            "Guarulhos",
            "BRA"
        ]
    ]
}
于 2013-07-28T14:31:10.250 回答
2

使用数字索引!任何以字符串为键的东西都必须产生花括号,因为这是在 javascript 中使用这些键的唯一方法。

json_encode(array("hello", "world")

["hello", "world"]
于 2013-07-28T14:22:14.000 回答
1

json 数组索引必须是数字。任何非数字索引都将转换为 json 对象。这就是为什么它被渲染成大括号。

关于您最初的问题,以下 PHP 代码: header("Content-type: application/json"); header("Access-Control-Allow-Origin", "*"); header("Access-Control-Allow-Methods: POST,OPTIONS"); header("Access-Control-Allow-Headers: Content-Type"); header("Access-Control-Allow-Credentials: false"); header("Access-Control-Max-Age: 60");

include "./inc/funclib.inc.php";
$array = array (
    "sEcho" =>  67,
    "iTotalRecords" => "4075",
    "iTotalDisplayRecords" => "4075",
    "aaData" => array (
        array ( "215", "Manaus", "BRA", "Amazonas", "1255049"),
        array ( "216", "Belem",  "BRA", "Para¡", "1186926"),
        array ( "217", "Guarulhos", "BRA", "Sao Paulo", "1095874")
    ));

echo jsonIndent(json_encode($array));

返回该输出:

{
  "sEcho":67,
  "iTotalRecords":"4075",
  "iTotalDisplayRecords":"4075",
  "aaData":[
    [
      "215",
      "Manaus",
      "BRA",
      "Amazonas",
      "1255049"
    ],
    [
      "216",
      "Belem",
      "BRA",
      "Para\u00a1",
      "1186926"
    ],
    [
      "217",
      "Guarulhos",
      "BRA",
      "Sao Paulo",
      "1095874"
    ]
  ]
}

这看起来像你正在寻找的东西。我希望这个能帮上忙 :-)

于 2013-07-28T14:56:03.063 回答
0

你有大括号,因为你指定了键和值,你只需要指定值。

$array = array(
    "foo" => "more foo",
    "st" => "bar",
    "ty" => "stuff",
    "multi" => array(
              "foo",
             "stuff"
         ),
         array(array(
              "foo",
             "stuff"
         )),
         array(array(
             "foo",
             "stuff"
         )),
         array(array(
              "foo",
             "stuff"
         ))
    );
于 2013-07-28T14:23:25.833 回答