强制 json_encode() 生成对象语法而不是数组语法

发布于 2024-08-06 12:19:57 字数 299 浏览 5 评论 0原文

我有一个简单的数组:

array
  0 => string 'Kum' (length=3)
  1 => string 'Kumpel' (length=6)

当我使用 json_encode() 对数组进行编码时,我得到以下信息:

["Kum","Kumpel"] 

我的问题是,为什么要获取 ["Kum","Kumpel"] 而不是 {“0”:“Kum”,“1”:“Kumpel”}

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:

["Kum","Kumpel"] 

My question is, what is the reason to get ["Kum","Kumpel"] instead of { "0" : "Kum", "1" : "Kumpel" }?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

み零 2024-08-13 12:19:57

根据 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"}

JSON_FORCE_OBJECT 强制它使用“0,1,2”的原因是因为要将数据分配给对象,您必须将其分配给属性,因为开发人员没有给出属性名称(仅提供数据),编码器使用数组索引作为属性名称,因为这些是唯一有意义的名称。

注意:根据 PHP 手册,选项参数仅在 PHP 5.3 中可用。

>对于较旧的 PHP 版本,请参阅 chelmertz 的答案,了解如何使 json_encode 使用索引。

"{}" 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.

$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.

Note: according to PHP manual the options parameters are only available from PHP 5.3.

For older PHP versions refer to chelmertz's answer for a way to make json_encode to use indexes.

落墨 2024-08-13 12:19:57

正如 Gumbo 所说,在 JS 方面这并不重要。要强制使用 PHP,请尝试以下操作:

$a = new stdClass();
$a->{0} = "Kum";
$a->{1} = "Kumpel";
echo json_encode($a);

不太可用,我会坚持使用数组表示法。

As Gumbo said, on the JS-side it won't matter. To force PHP into it, try this:

$a = new stdClass();
$a->{0} = "Kum";
$a->{1} = "Kumpel";
echo json_encode($a);

Not that usable, I'd stick with the array notation.

淡看悲欢离合 2024-08-13 12:19:57

只需将其转换为对象即可正常工作... JSON_FORCE_OBJECT 参数执行完全相同的操作。

json_encode((object)$array);

不要忘记将其转换回 php 数组,以便您可以在 php 中访问它的值:

$array = (object)$array;
$array = (array)$array;

json_encode($array);

Just cast as an object and it will work fine...the JSON_FORCE_OBJECT parameter does exactly the same thing.

json_encode((object)$array);

Don't forget to convert it back into a php array so you can access its values in php:

$array = (object)$array;
$array = (array)$array;

json_encode($array);
信仰 2024-08-13 12:19:57

由于您有一个仅包含数字键的 PHP 数组,因此无需使用 JavaScript 对象。但如果您需要一个,请尝试 Maiku Mori 的建议。

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.

等待圉鍢 2024-08-13 12:19:57

我个人认为这是 PHP 中需要修复的一个 bug。 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]}

然而 PHP 希望我接受

[[0,1,2,3 ],[4,5,6,7]]

{"0":{"0":1,"1":1,"2":2,"3":3},"1":{" 0":4,"1":5,"2":6,"3":7}}

两者都不正确。我怎样才能解码这样的对象?有什么可能的原因来改变明显使用字符串作为索引的东西?这就像 PHP 试图聪明地帮助那些无法区分字符串和整数的白痴,但在这个过程中,任何合法使用字符串作为索引的人都陷入困境,无论值可以变成什么。

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:

array("0" => array(0,1,2,3), "1" => array(4,5,6,7));

And should be converted to

{"0": [0,1,2,3], "1": [4,5,6,7]}

Yet PHP expects me to either accept

[[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}}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文