PHP中的json_decode是否保证在返回数组时保留元素的排序?

时间:2022-10-26 13:59:03

You can pass a boolean to json_decode to return an array instead of an object

您可以将布尔值传递给json_decode以返回数组而不是对象

json_decode('{"foo", "bar", "baz"}', true);  // array(0 => 'foo', 1 => 'bar', 2 => 'baz')

My question is this. When parsing object literals, does this guarantee that the ordering of the items will be preserved? I know JSON object properties aren't ordered, but PHP arrays are. I can't find anywhere in the PHP manual where this is addressed explicitly. It probably pays to err on the side of caution, but I would like to avoid including some kind of "index" sub-property if possible.

我的问题是这个。在解析对象文字时,这是否可以保证项目的顺序将被保留?我知道JSON对象属性没有排序,但PHP数组是。我无法在PHP手册中的任何地方找到明确解决的问题。它可能在谨慎方面付出了代价,但我想尽可能避免包含某种“索引”子属性。

4 个解决方案

#1


4  

Wouldn't it make more sense in this case to use an array when you pass the JSON to PHP. If you don't have any object keys in the JSON (which become associative array keys in PHP), just send it as an array. That way you will be guaranteed they will be in the same order in PHP as in javascript.

在这种情况下,在将JSON传递给PHP时使用数组会不会更有意义。如果JSON中没有任何对象键(在PHP中成为关联数组键),只需将其作为数组发送即可。这样你就可以保证他们在PHP中与javascript中的顺序相同。

json_decode('{["foo", "bar", "baz"]}');
json_decode('["foo", "bar", "baz"]'); //I think this would work

If you need associative arrays (which is why you are passing the second argument as true), you will have to come up with some way to maintain their order when passing. You will pry have to do some post-processing on the resulting array after you decode it to format it how you want it.

如果你需要关联数组(这就是你将第二个参数传递为true的原因),你必须想出一些方法来在传递时保持它们的顺序。在解码之后,您将撬开必须对生成的数组进行一些后处理,以便按照您希望的方式对其进行格式化。

$json = '{[ {"key" : "val"}, {"key" : "val"} ]}';
json_decode($json, true);

#2


1  

Personally, I've never trusted any system to return an exact order unless that order is specifically defined. If you really need an order, then use a dictionary aka 2dimension array and assigned a place value (0,1,2,3...) to each value in the list.

就个人而言,我从不信任任何系统返回确切的订单,除非该订单是专门定义的。如果您确实需要订单,则使用字典aka 2dimension数组并为列表中的每个值分配一个位值(0,1,2,3 ...)。

If you apply this rule to everything, you'll never have to worry about the delivery/storage of that array, be it XML, JSON or a database.

如果将此规则应用于所有内容,您将永远不必担心该阵列的交付/存储,无论是XML,JSON还是数据库。

Remember, just because something happens to work a certain way, doesn't mean it does so intentionally. It's akin to thinking rows in a database have an order, when in fact they don't unless you use an ORDER BY clause. It's unsafe to think ID 1 always comes before ID 2 in a SELECT.

请记住,仅仅因为某些事情以某种方式发生,并不意味着它故意这样做。它类似于认为数据库中的行具有顺序,而实际上它们不会使用ORDER BY子句。认为ID 1始终位于SELECT 2中的ID 2之前是不安全的。

#3


1  

I've used json_decode() some times, and the results order was kept intact with PHP client apps. But with Python for instance it does not preserve the order.

我曾经多次使用json_decode(),结果顺序与PHP客户端应用程序保持完整。但是以Python为例,它并没有保留订单。

One way to be reassured is to test it over with multiple examples.

一种可以放心的方法是用多个例子来测试它。

#4


1  

Lacking an explicit statement I'd say, by definition, no explicit order will be preserved.

缺乏明确的陈述我会说,根据定义,不会保留明确的顺序。

My primary line of thought it what order, exactly, would this be preserving? The json_decode function takes a string representation of a javascript object literal as it's argument, and then returns either an object or an array. The function's input (object literal as string) has no explicit ordering, which means there's no clear order for the json_decode function to maintain.

我的主要思路是什么顺序,确切地说,这将保留? json_decode函数将javascript对象文字的字符串表示作为其参数,然后返回对象或数组。函数的输入(对象文字作为字符串)没有显式排序,这意味着json_decode函数没有明确的维序。

#1


4  

Wouldn't it make more sense in this case to use an array when you pass the JSON to PHP. If you don't have any object keys in the JSON (which become associative array keys in PHP), just send it as an array. That way you will be guaranteed they will be in the same order in PHP as in javascript.

在这种情况下,在将JSON传递给PHP时使用数组会不会更有意义。如果JSON中没有任何对象键(在PHP中成为关联数组键),只需将其作为数组发送即可。这样你就可以保证他们在PHP中与javascript中的顺序相同。

json_decode('{["foo", "bar", "baz"]}');
json_decode('["foo", "bar", "baz"]'); //I think this would work

If you need associative arrays (which is why you are passing the second argument as true), you will have to come up with some way to maintain their order when passing. You will pry have to do some post-processing on the resulting array after you decode it to format it how you want it.

如果你需要关联数组(这就是你将第二个参数传递为true的原因),你必须想出一些方法来在传递时保持它们的顺序。在解码之后,您将撬开必须对生成的数组进行一些后处理,以便按照您希望的方式对其进行格式化。

$json = '{[ {"key" : "val"}, {"key" : "val"} ]}';
json_decode($json, true);

#2


1  

Personally, I've never trusted any system to return an exact order unless that order is specifically defined. If you really need an order, then use a dictionary aka 2dimension array and assigned a place value (0,1,2,3...) to each value in the list.

就个人而言,我从不信任任何系统返回确切的订单,除非该订单是专门定义的。如果您确实需要订单,则使用字典aka 2dimension数组并为列表中的每个值分配一个位值(0,1,2,3 ...)。

If you apply this rule to everything, you'll never have to worry about the delivery/storage of that array, be it XML, JSON or a database.

如果将此规则应用于所有内容,您将永远不必担心该阵列的交付/存储,无论是XML,JSON还是数据库。

Remember, just because something happens to work a certain way, doesn't mean it does so intentionally. It's akin to thinking rows in a database have an order, when in fact they don't unless you use an ORDER BY clause. It's unsafe to think ID 1 always comes before ID 2 in a SELECT.

请记住,仅仅因为某些事情以某种方式发生,并不意味着它故意这样做。它类似于认为数据库中的行具有顺序,而实际上它们不会使用ORDER BY子句。认为ID 1始终位于SELECT 2中的ID 2之前是不安全的。

#3


1  

I've used json_decode() some times, and the results order was kept intact with PHP client apps. But with Python for instance it does not preserve the order.

我曾经多次使用json_decode(),结果顺序与PHP客户端应用程序保持完整。但是以Python为例,它并没有保留订单。

One way to be reassured is to test it over with multiple examples.

一种可以放心的方法是用多个例子来测试它。

#4


1  

Lacking an explicit statement I'd say, by definition, no explicit order will be preserved.

缺乏明确的陈述我会说,根据定义,不会保留明确的顺序。

My primary line of thought it what order, exactly, would this be preserving? The json_decode function takes a string representation of a javascript object literal as it's argument, and then returns either an object or an array. The function's input (object literal as string) has no explicit ordering, which means there's no clear order for the json_decode function to maintain.

我的主要思路是什么顺序,确切地说,这将保留? json_decode函数将javascript对象文字的字符串表示作为其参数,然后返回对象或数组。函数的输入(对象文字作为字符串)没有显式排序,这意味着json_decode函数没有明确的维序。