如何将 PHP 数组编码为 JSON 数组,而不是对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/684262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I encode a PHP array to a JSON array, not object?
提问by Itay Moav -Malimovka
I am trying to json_encode an array which is returned from a Zend_DB query.
我正在尝试对从 Zend_DB 查询返回的数组进行 json_encode。
var_dump gives: (Manually adding 0 member does not change the picture.)
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"
}
}
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"}}
What I need is:
我需要的是:
[{"comment_id":"1","erasable":"1","comment":"test 1"},
{"comment_id":"2","erasable":"1","comment":"test 1"},
{"comment_id":"3","erasable":"1","comment":"jhghjg"}]
Which is what the php.ini/json_encode documentation says it should look like.
这就是 php.ini/json_encode 文档所说的它应该是什么样子。
回答by Seb
How are you setting up your initial array?
您如何设置初始阵列?
If you set it up like:
如果你这样设置:
array(
"1" => array(...),
"2" => array(...),
);
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).
那么你没有一个带有数字索引而是字符串的数组,这在 JS 世界中被转换为一个对象。如果您没有设置严格的顺序(即从 0 而不是 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(...),
);
回答by Kent Fredric
Added information that expands on Seb's answer.
添加了扩展Seb答案的信息。
php > print json_encode( array( 'a', 'b', 'c' ) ) ;
["a","b","c"]
php > print json_encode( array( 0 => 'a', 1 => 'b', 2 => 'c' ) ) ;
["a","b","c"]
php > print json_encode( array( 1 => 'a', 2 => 'b', 3 => 'c' ) ) ;
{"1":"a","2":"b","3":"c"}
php >
Note: its formatting it this way with good cause:
注意:它以这种方式格式化是有充分理由的:
If you were to send
如果你要发送
{"1":"a","2":"b","3":"c"}
as
作为
["a","b","c"]
When you did $data[1]in Php you would get back "a", but on the JavaScript side, you would get back "b" .
当你没有 $data[1]在PHP中还是会回到“一个”,但在JavaScript端,你还是会回到“B”。
回答by Richard Levasseur
A common way to test for a traditional, continuous array in php is to check for an index '0'. Try adding that to your array, it'll probably considering it an array instead of hashmap.
在 php 中测试传统连续数组的常用方法是检查索引“0”。尝试将其添加到您的数组中,它可能会将其视为数组而不是哈希图。
回答by harry
i had a similar problem, got it to work after adding '' (single quotes) around the json_encode string. Following from my js file:
我有一个类似的问题,在 json_encode 字符串周围添加 '' (单引号)后让它工作。以下来自我的 js 文件:
var myJsVar = <?php echo json_encode($var); ?> ; -------> NOT WORKING
var myJsVar = '<?php echo json_encode($var); ?>' ; -------> WORKING

