防止json_encode关联数组排序 [英] Prevent json_encode associative array sorting
本文介绍了防止json_encode关联数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个关联数组
Array(
[289] => Array(
'name'=> 'One'
),
[292] => Array(
'name'=> 'One'
),
[290] => Array(
'name'=> 'One'
)
)
在此数组上使用json_encode之后.键是经过排序的,尽管我将其作为JSON对象获取.
After i use json_encode on this array. The keys are sorted, although i get it as JSON object.
有没有办法防止这种行为?
Is there way to prevent this behaviour?
推荐答案
没有标准规定必须按一定顺序进行.
there is no standard that says it has to be in a certain order.
See this for a related question: How do you stop Chrome and Opera sorting JSON objects by Index ASC?
注意:我们正在谈论的是PHP函数,但结果基本上是javascript,因此有关不存在的标准的声明也适用.
btw:我已经使用以下代码对其进行了测试. PHP本身似乎没有对数组进行排序,firefox也没有(根据firebug控制台).
btw: I have tested it with the following code. PHP itself doesnt seem to sort the array, firefox doesn't as well (according to the firebug console).
<pre>
<?php
$array = array();
$array[289] = array('name'=>'One');
$array[292] = array('name'=>'One');
$array[290] = array('name'=>'One');
print_r($array);
$string = json_encode($array);
print_r($string);
?>
</pre>
<script>
var foo = <?=$string?>;
console.log(foo);
</script>
这篇关于防止json_encode关联数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文