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.