gpt4 book ai didi

javaScript 对象不保留 php 给出的关键命​​令

转载 作者:行者123 更新时间:2023-11-28 15:42:59 25 4
gpt4 key购买 nike

我有一个使用 json_encode 的数组,在 PHP 中看起来像这样

Array
(
[4] => Array
(
[numberof] => 60
[date] => 4
)

[3] => Array
(
[numberof] => 3
[date] => 3
)

[2] => Array
(
[numberof] => 6
[date] => 2
)

[1] => Array
(
[numberof] => 5
[date] => 1
)

[12] => Array
(
[numberof] => 1
[date] => 12
)

[11] => Array
(
[numberof] => 0
[date] => 11
)

)

但是,当通过javaScript访问时,顺序如下

{
"1":{"numberof":5,"date":"1"},
"2":{"numberof":6,"date":"2"},
"3":{"numberof":3,"date":"3"},
"4":{"numberof":60,"date":"4"},
"11":{"numberof":0,"date":"11"},
"12":{"numberof":1,"date":"12"}
}

如何保持 PHP 中设置的顺序。

最佳答案

JavaScript 对象没有排序。

ECMA 5.1 section 4.3.3 (不保证订单)

An object is a collection of properties and has a single prototype object. The prototype may be the null value.

ECMA 3rd Edition section 4.3.3 (明确表示未订购)

An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

即使使用 JavaScript “关联数组”(基本上是对象中命名属性的集合),您也无法保证顺序。

关于javaScript 对象不保留 php 给出的关键命​​令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23230424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com