jQuery.parseJSON无序数组

Initial PHP ARRAY Storing attributes as values and respective id's on key

Array
(
[979] => Pict Model
[962] => Brand
[494] => Dimensions
[980] => Capacity
[981] => Power
[982] => List Price
[983] => Warrenty
[975] => USB Connection
[976] => Self Cleaning
[977] => Double Glass Door
[978] => Steam Function
[974] => Electricity Type
)

In my Code below, comparable_attr holds the json encoded array. After that, as i console.log(comparable_attr) This gives me a json of order as of php array.

Then, after i parseJSON and then console.log it gave me data in different order.

var comparable_attr = '<?php echo json_encode($_comparable_attributes); ?>';
   if(comparable_attr.length != 0){       //check for empty json
      console.log(comparable_attr);
          var obj = jQuery.parseJSON(comparable_attr);
                console.log(obj);
}

Problem : i want to achieve it in same order after i parseJSON.

The result obtained :

First Console.log(comparable_attr) gives:

{"979":"Pict Model","962":"Brand","494":"Dimensions","980":"Capacity","981":"Power","982":"List Price","983":"Warrenty","975":"USB Connection","976":"Self Cleaning","977":"Double Glass Door","978":"Steam Function","974":"Electricity Type"}

Second Console.log(obj) gives :

 Object { 494="Dimensions", 962="Brand", 974="Electricity Type", more...}

enter image description here

Edited :

What i have found is, it shorted according to my attribute id, which i need in the same order as it is in array.

When you convert a PHP spare array into JSON and subsequently into a JS variable you will get a JS Object, not an array, and JS objects are explicitly an unordered set of key value pairs.

If you wish to preserve the order you will need to rearrange the data on the PHP side first, perhaps as:

Array(
    Array([979] => Pict Model),
    Array([962] => Brand),
    ...
)

Note however that if you do this you will lose the ability to directly lookup the data by key, as the resulting JS object would look like:

[
  { "979": "Pict Model" },  // NB: all JS keys are strings
  { "962": "Brand" },
  ...
]

An alternative might be to send your existing array as is, but also send a second array that is just the ordered list of keys. The former can be used for key-based lookup, the latter to determine the correct order:

json_encode(array(
    'data' => $_comparable_attributes,
    'order' => array_keys($_comparable_attributes)
));

Javascript’s json object order is inplementation dependant. You could try it in the console:

a = {2:'a', 1:'b'}
Object {1: "b", 2: "a"}

To solve your problem I would suggest you to prevent auto sorting by:

array_reduce(
  $a, 
  function($carry, $item) use(&$a) { 
    $carry[] = [key($a), $item]; next($a); return $carry; 
  }, 
  []
)

This will wrap your initial array into array containing [key, val] items.

Hope this helps.