php json_encode() 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1515925/
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
Problem with json_encode()
提问by cupakob
i have an simple array:
我有一个简单的数组:
array
0 => string 'Kum' (length=3)
1 => string 'Kumpel' (length=6)
when I encode the array using json_encode(), i get following:
当我使用 json_encode() 对数组进行编码时,我得到以下信息:
["Kum","Kumpel"]
My question is, what is the reason to get ["Kum","Kumpel"]instead of { "0" : "Kum", "1" : "Kumpel" }?
我的问题是,获取["Kum","Kumpel"]而不是的原因是什么{ "0" : "Kum", "1" : "Kumpel" }?
回答by Maiku Mori
"{}" brackets specify an object and "[]" are used for arrays according to JSON specification. Arrays don't have enumeration, if you look at it from memory allocation perspective. It's just data followed by more data, objects from other hand have properties with names and the data is assigned to the properties, therefore to encode such object you must also pass the correct property names. But for array you don't need to specify the indexes, because they always will be 0..n, where n is the length of the array - 1, the only thing that matters is the order of data.
"{}" 括号指定一个对象,"[]" 用于根据 JSON 规范的数组。如果从内存分配的角度来看,数组没有枚举。它只是数据后跟更多数据,来自其他方面的对象具有带名称的属性,并且数据被分配给属性,因此要对此类对象进行编码,您还必须传递正确的属性名称。但是对于数组,您不需要指定索引,因为它们总是 0..n,其中 n 是数组的长度 - 1,唯一重要的是数据的顺序。
$array = array("a","b","c");
json_encode($array); // ["a","b","c"]
json_encode($array, JSON_FORCE_OBJECT); // {"0":"a", "1":"b","2":"c"}
The reason why JSON_FORCE_OBJECT foces it to use "0,1,2" is because to assign data to obeject you must assign it to a property, since no property names are given by developer (only the data) the encoder uses array indexes as property names, because those are the only names which would make sense.
JSON_FORCE_OBJECT 使用“0,1,2”的原因是因为要将数据分配给对象,您必须将其分配给一个属性,因为开发人员没有给出属性名称(只有数据)编码器使用数组索引作为属性名称,因为这些是唯一有意义的名称。
Note: according to PHP manualthe options parameters are only available from PHP 5.3.
注意:根据PHP 手册,选项参数仅适用于 PHP 5.3。
For older PHP versions refer to chelmertz's answer for a way to make json_encode to use indexes.
对于较旧的 PHP 版本,请参阅 chelmertz 的答案,以获取使 json_encode 使用索引的方法。
回答by chelmertz
As Gumbo said, on the JS-side it won't matter. To force PHP into it, try this:
正如 Gumbo 所说,在 JS 方面这无关紧要。要强制 PHP 进入它,请尝试以下操作:
$a = new stdClass();
$a->{0} = "Kum";
$a->{1} = "Kumpel";
echo json_encode($a);
Not that usable, I'd stick with the array notation.
不是那么可用,我会坚持使用数组表示法。
回答by TaylorMac
Just cast as an object and it will work fine...the JSON_FORCE_OBJECT parameter does exactly the same thing.
只需将其转换为对象,它就可以正常工作…… JSON_FORCE_OBJECT 参数完全相同。
json_encode((object)$array);
Don't forget to convert it back into a php array so you can access its values in php:
不要忘记将其转换回 php 数组,以便您可以在 php 中访问它的值:
$array = (object)$array;
$array = (array)$array;
json_encode($array);
回答by Chris Nasr
I personally think this is a bug that needs to be fixed in PHP. JSON_FORCE_OBJECT is absolutely not an answer. If you try to do any sort of generic programming you get tripped up constantly. For example, the following is valid PHP:
我个人认为这是一个需要在 PHP 中修复的错误。JSON_FORCE_OBJECT 绝对不是答案。如果您尝试进行任何类型的通用编程,您就会经常被绊倒。例如,以下是有效的 PHP:
array("0" => array(0,1,2,3), "1" => array(4,5,6,7));
数组("0" => 数组(0,1,2,3), "1" => 数组(4,5,6,7));
And should be converted to
并且应该转换为
{"0": [0,1,2,3], "1": [4,5,6,7]}
{"0": [0,1,2,3], "1": [4,5,6,7]}
Yet PHP expects me to either accept
然而 PHP 希望我要么接受
[[0,1,2,3],[4,5,6,7]]
[[0,1,2,3],[4,5,6,7]]
or
或者
{"0":{"0":1,"1":1,"2":2,"3":3},"1":{"0":4,"1":5,"2":6,"3":7}}
{"0":{"0":1,"1":1,"2":2,"3":3},"1":{"0":4,"1":5,"2 ":6,"3":7}}
Neither of which are right at all. How can I possibly decode an object like that? What possible reason is there to ever change something that is clearly using strings as indexes? It's like PHP was trying to be clever to help out idiotic people who can't differentiate strings from ints, but in the process messed up anyone legitimately using strings as indexes, regardless of what the value COULD be turned into.
这两者都不对。我怎么可能解码这样的对象?有什么可能的原因改变明显使用字符串作为索引的东西?这就像 PHP 试图聪明地帮助那些无法区分字符串和整数的白痴,但在这个过程中,任何合法地使用字符串作为索引的人都搞砸了,无论值可以变成什么。
回答by Gumbo
Since you're having a PHP array with just numeric keys, there is no need to use a JavaScript object. But if you need one, try Maiku Mori's suggestion.
由于您拥有一个只有数字键的 PHP 数组,因此无需使用 JavaScript 对象。但如果你需要一个,试试 Maiku Mori 的建议。

