2014-05-23 03:05
浏览 459

PHP json_decode()是否总是保留JSON数组的数组顺序?

Does the string


always preserve array order and result in the PHP array


when using json_decode()? I realize the answer is NO for objects, but I am asking about a string representing an array as input.

图片转代码服务由CSDN问答提供 功能建议


  [“first”,“second  “,”第三个“] 


 数组(' 第一个','秒','第三个'); 

使用 json_decode()时? 我意识到答案是对象没有,但我问的是一个表示数组的字符串作为输入。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongyunqin7307 2014-05-23 03:10

    Yes. Arrays are ordered by definition, and JSON preserves this.

    The JSON specification says:

    An array structure is a pair of square bracket tokens surrounding zero or more values. The values are separated by commas. The order of the values is significant.

    The last sentence implies that a JSON encoder or decoder that changes the order is not in conformance with the specification. I can't find anything in the PHP documentation that explicitly says that it observes this requirement, but I think it can be assumed since it claims to be implementing JSON.

    解决 无用
    打赏 举报

相关推荐 更多相似问题