Will the order of my associative array with PHP in Javascript be maintained?

In PHP, I run mysql_query with an ORDER BY clause. Then I repeat the results to create an associative array with the row_id key.

Then I call json_encode in this array and print the result.

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

+5
source share
2 answers

cletus, , , , . Array. , , .sort() JavaScript:

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

, , . . ( , ). , :

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

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

( Firefox 3.6.3 Chrome 5.0.375.9):

one
two
another

, , JSON, json_encode(), ( {} ), ([] ). JSON_FORCE_OBJECT json_encode(), .

  • , , Array )
  • (), pcorcoran, Chromium tracker. , .
+5

PHP . Javascript . , . - .

? . - , ?

+11

All Articles