Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

PHP:

 $arr[0] = 'A';
 $arr['C'] = 'C';
 $arr[10] = 'B';

 echo json_encode($arr);

JQuery:

 $.each(result, function(i, item) {
    console.log(i + " => " + item);
 });

Desired Output:

   0 => A
   C => C
   10 => B

Instead I Get:

   0 => A
   10 => B
   C => C

how can i prevent it to re-order my array without modifying the PHP code or restructuring the array?

Edit:

when ajax is called in response headers using firebug it seems to be in correct order:

"0":"A","C":"C","10":"B"

however when I do console.log inside $.each loop its re-ordered

share|improve this question
That's no array, that's an object. And object keys are not ordered. If you need to preserve order, use an array. – Bergi Apr 22 at 18:52
but how do i do it when i need to pass it from PHP to Javascript ? – GGio Apr 22 at 18:53
I just told you: use an array - only numeric keys, and iterate them with a for (var i=0; i<arr.length; i++) loop – Bergi Apr 22 at 18:54
not an option since its not up to me. The array has non-numeric keys :( – GGio Apr 22 at 18:56
Then you can do nothing but: Extract the keys from the object and sort() them in some way (if their order is known) or manually parse the JSON string, using arrays instead of objects. And tell the one who is responsible for the JSON that objects in JSON are specified to be unordered… – Bergi Apr 22 at 18:59

1 Answer

Your $arr is an object, not an array and the keys aren't indexed nor ordered.

You don't have guarantee in JavaScript about the iteration order on object properties, only the indexed keys (i.e. integer keys) of arrays.

To iterate over a plain object, $.each uses the standard for..in construct on which the MDN precises that

A for...in loop iterates over the properties of an object in an arbitrary order

If you want to keep arbitrary key-value ordered, you should store both in a proper array :

  var arr = [];
  arr.push({key:0, value:'A'});
  arr.push({key:'C', value:'C'});
  arr.push({key:10, value:'B'});
share|improve this answer
but im doing it on PHP side how can i do this using PHP? – GGio Apr 22 at 18:49
@GGio: The code above creates an empty array and adds some items. You don't know how to do that in PHP? Why not read the manual? – Jon Apr 22 at 19:05
I don't remember how to do that in PHP. If somebody wants to complete my answer, I made it CW. – dystroy Apr 22 at 19:09
@Jon if you look at my code it has an array that contains exact keys and values but the problem is once used $.each loop on it when passed to Javascript it re-orders it. – GGio Apr 22 at 19:13
1  
@GGio: I have read everything on this page, and both dystroy and Bergi have given an accurate answer to your question. But you need to work a little yourself. – Jon Apr 22 at 19:15
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.