Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 740403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:30:43+00:00 2026-05-14T08:30:43+00:00

In PHP I’m running a mysql_query that has an ORDER BY clause. I’m then

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:30:43+00:00Added an answer on May 14, 2026 at 8:30 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP has PHP-FPM, a pool of PHP processes that run, are managed, go away
PHP has some pretty inconsistent parameters for functions (e.g. sometimes first haystack , then
PHP: I have made up a function that returns an array. I would like
PHP Code <?php $content = check1\r\ncheck2\r\ncheck3\r\nend... $order = array(\r\n); $replace = \n; $content= str_replace(
PHP - Access MSSQL datetime column from the returned array Array ( [0] =>
I want to count how many characters a certain string has in PHP, but
PHP: How to share data between threads on different servers? Suppose server has opened
PHP_CodeCoverage 1.1 removed the singleton accessor for PHP_CodeCoverage_Filter that allowed our PHPUnit bootstrap.php files
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
PHP's documentation on this function is a bit sparse and I have read that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.