views:

75

answers:

2

In PHP I'm running a mysql_query that has an ORDER BY clause. I'm then iterating through the results to build an associative array, with the row_id as the key.

Then, I'm calling json_encode on that array and outputting the result.

This page is loaded with AJAX, and defined in a Javascript variable. When I iterate through that Javascript variable, will I still have the order that was returned from the mysql_query?

+10  A: 

PHP arrays are somewhat unique in their property of maintaining insertion order. Javascript doesn't have associative arrays per se. It has objects, which are often used as associative arrays. These do not guarantee any particular key order.

Why not output them as an array? That will have a particular order. If you want some sort of key lookup why does the order matter?

cletus
I think last I checked, ecma spec said it was unordered, but all browsers implemented them as being ordered.
chris
All browsers used to implement object associative arrays as ordered-by-age. Chrome is now the exception. And since ECMA also says order need not be preserved, it's best not to rely on order.See: http://code.google.com/p/chromium/issues/detail?id=883 for discussion re: Chrome.
pcorcoran
+4  A: 

What cletus says is correct, but in my experience, most browsers will maintain the order. That being said, you should consider using an Array. If you need to sort it once you receive it on the client-side, just use the .sort() function in JavaScript:

rows.sort(function(a, b) {
    return a.row_id - b.row_id;
}

Though it seems like it works, the order of properties in an object can't be counted on. See the many comments below for more info (smarter eyes than mine). However, this was the code I used to test the behavior in my own limited testing:

var test = {
    one: 'blah',
    two: 'foo',
    another: 'bar'
};

for (prop in test) {
    document.write(prop + "<br />");
}

Prints (in Firefox 3.6.3 and Chrome 5.0.375.9):

one
two
another

Also, you may want to be sure you're getting the type of JSON encoding you're needing back from json_encode(), such as an object (uses {} curly braces) and not an array ([] braces). You may need to pass JSON_FORCE_OBJECT to json_encode() to force it.

  • Edited to clarify that the Array approach is preferred)
  • Edited again (sorry), as I had overlooked pcorcoran's comment, which has a link to an issue in Chromium's issue tracker regarding this. Suffice to say, the order an object's properties is not reliable.
awgy
Already fails on Opera and Chrome, and behaviour elsewhere may change as JS engines continue to be optimised. Don't rely on Object order: go for the Array.
bobince
It seems to work for me in Chrome (5.0.375.9 on Mac OS X)-- are you using a different version, or do you have a snippet of code that could cause Chrome to disregard the order? I'm mostly interested from just a curiosity standpoint. I agree with you-- the best approach would be to use an array and `.sort()` if needed.
awgy
Nevermind-- overlooked pcorcoran's comment below, linking to the Chromium issue tracker.
awgy