11

I have a associative array

Array(
   [289] => Array(
    'name'=> 'One'
   ),
   [292] => Array(
    'name'=> 'One'
   ),
   [290] => Array(
    'name'=> 'One'
   )
)

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?

rbawaskar
  • 986
  • 2
  • 9
  • 22
  • Looks like php json_encode is return the correct order. Its the browser which is sorting the object. – rbawaskar Jan 19 '14 at 11:59
  • Are you sure? For me PHP's json_encode sorts the array, rather than the browsers. – thomasrutter Dec 10 '15 at 05:46
  • Check out JSON_FORCE_OBJECT in http://php.net/manual/en/json.constants.php - I had an associative array with numeric ID's as the keys and it was converting them as a non-associative array – Sp4cecat May 24 '16 at 02:30

2 Answers2

6

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?

note: we're talking about a PHP function, but the result is basically javascript, so the statement about the non-existing standard applies as well.

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>
Community
  • 1
  • 1
Zim84
  • 3,116
  • 2
  • 29
  • 37
  • 2
    This is the correct answer. If you want your own order, give the array your own index. – Christian Jan 19 '14 at 12:03
  • +1 , for the right thing. – Shankar Damodaran Jan 19 '14 at 12:03
  • 1
    Yes, its browser which is culprit – rbawaskar Jan 20 '14 at 05:36
  • 2
    Note: firefox will sort keys if the keys are numeric and under something like 500. Javascript objects are not guaranteed to have ordering, this is a mistake a lot of people, most languages associative arrays are not ordered by entry. If you want order, use ordered arrays. https://bugzilla.mozilla.org/show_bug.cgi?id=865760 – Rahly Nov 23 '15 at 20:14
-4

Try this:

    $ar = array();
    $ar[1] = array('1'=>'one');
    $ar[2] = array('2'=>'two');
    $ar[3] = array('3'=>'three');
    print_r($ar);
    $str= json_encode($ar);
    print_r($str);

it should work, at-least helps me !

Aditya P Bhatt
  • 19,393
  • 17
  • 78
  • 102
  • It doesn't work like that, i have a non associative array where i need to preserve the keys but order alphabetichally by Value, using Asort it gets ordered properly but when sent to using json_encode the object gets sorted by key again – Danny22 Dec 26 '14 at 22:02
  • That's exactly the same problem I'm having, been looking for a solution everywhere but no luck yet. – Chaoley Mar 31 '16 at 13:30