[英]convert json with square brackets to php array
I am trying to discover what php array gets you this json 我试图发现什么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"
]
]
}
I am trying this 我正在尝试
$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>";
but my json has a curly bracket 但我的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"}]}
{“ 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”}]}
How can i correct my array to produce the json?. 我如何纠正我的数组以产生json?
You have too many arrays of depth, and you shouldn't specify the key. 您有太多的深度数组,因此不应该指定键。 In PHP arrays if you set a string key and JSON encode it, it will produce an object not an array.
在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);
Outputs: 输出:
{
"foo": "more foo",
"st": "bar",
"ty": "stuff",
"multi": [
[
"215",
"Manaus",
"BRA"
],
[
"216",
"Belém",
"BRA"
],
[
"217",
"Guarulhos",
"BRA"
]
]
}
Use numeric indices! 使用数字索引! Anything with a string as a key must produce curly braces, because thats the only way to have these keys in javascript.
任何以字符串为键的东西都必须产生花括号,因为那是在JavaScript中使用这些键的唯一方法。
json_encode(array("hello", "world")
["hello", "world"]
json array indexes MUST be numerical. json数组索引必须为数字。 Any non numerical index will be converted into a json object.
任何非数字索引都将转换为json对象。 That's why it is rendered into curly brackets .
这就是为什么将其呈现为大括号。
About your initial question, the following PHP code : header("Content-type: application/json"); 关于您的最初问题,以下PHP代码:header(“ Content-type:application / json”); header("Access-Control-Allow-Origin", "*");
header(“ Access-Control-Allow-Origin”,“ *”); header("Access-Control-Allow-Methods: POST,OPTIONS");
header(“ Access-Control-Allow-Methods:POST,OPTIONS”); header("Access-Control-Allow-Headers: Content-Type");
header(“ Access-Control-Allow-Headers:Content-Type”); header("Access-Control-Allow-Credentials: false");
header(“ Access-Control-Allow-Credentials:false”); header("Access-Control-Max-Age: 60");
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));
returns that output: 返回该输出:
{
"sEcho":67,
"iTotalRecords":"4075",
"iTotalDisplayRecords":"4075",
"aaData":[
[
"215",
"Manaus",
"BRA",
"Amazonas",
"1255049"
],
[
"216",
"Belem",
"BRA",
"Para\u00a1",
"1186926"
],
[
"217",
"Guarulhos",
"BRA",
"Sao Paulo",
"1095874"
]
]
}
Which looks like what you are looking for. 看起来像您要找的东西。 I hope this will help :-)
我希望这个能帮上忙 :-)
you have curly brackets because you specified both keys and values, you will need to specify just values. 您有大括号,因为您同时指定了键和值,因此只需要指定值即可。
$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"
))
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.