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

  • Home
  • SEARCH
  • 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 6098145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:07:28+00:00 2026-05-23T13:07:28+00:00

I have a php script that fetches data from a DB, the result set

  • 0

I have a php script that fetches data from a DB, the result set is returned in a particular order, e.g.

Name: John, Age: 21, Address: 1234 Fifth Ave.

I verified the order while debugging. The result set is then pushed into an array, which is then encoded as a json object and passed to my jquery script through an AJAX request.

var resultSet = [];
    $.ajax({
        async: false,
        url: 'scripts/getData.php,
        dataType: "json",
        success: function(data) {
        /* Store the pieces of the array */
        resultSet = data["php_resultSet"]; //<--Breakpoint here shows the resultSet sorted, if order is preserved here, my problem will be solved.
    }
    });

However, when I get my data back and analyze the array, it is sorted, e.g.

Address: 1234 Fifth Ave., Age: 21, Name: John Age: 21

I want to preserve the order of the original result set, is there an option I have to set in order to do that? Help is appreciated.

UPDATE:

Here is a snippet of the PHP code that builds the array and encodes it

    $data = array ( "otherData" => array(), "php_resultSet" => array() );

    while ( $row = sqlsrv_fetch_array ( $stmt, SQLSRV_FETCH_ASSOC ) ) {
        //Push the entire row to the result set array
        array_push ( $data["php_resultSet"], $row );
    }

    asort($someData);

    foreach ( $someData as $key) {
        array_push ( $data["otherData"], $key );
    }

return json_encode($data);//<--At this point, even though the I sorted $someData before pushing it to the $data, the php_resultSet maintains its sorted order.
  • 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-23T13:07:29+00:00Added an answer on May 23, 2026 at 1:07 pm

    When encoding a hash (associative array) into JSON, the order of key/value pairs is not guaranteed to be preserved due to differences in how a hash is represented in the source language. For example, hashes in Perl are stored in such a manner as to optimize storage requirements at the expense of field order.

    Having said that, there are several approaches you can take to have JavaScript reorder the AJAX result, one being to return the result along with the expected order of presentation, as in:

    {
        "presentation_order": [
            "Name",
            "Address",
            etc.
         ],
         "records": [
             {"Name":"Juan", etc.},
             etc.
         ]
    }
    

    And then use it to either reorder the records before or while you populate the table on which they are expected to appear.

    Another approach might be to append a value to each field name in each record which can be used to order them after retrieval and then, before display, removing the ordering value from the field name.

    So on the server after converting the result into JSON, add a prefix from “aa_” for the first fieldname, incrementing the prefix for each succeeding field name up to “zz_”. So if the first two field names are “Name” and “Address” then become “aa_Name” and “ab_Address”.

    Then just before adding the record to the table at the client, sort the fields by field name and then remove the prefix using fieldName=fieldName.substring(3).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ajax script, in the main index.php page, that fetches data from
I have a server-side php-script that fetches results from a MySQL table. I am
I have a php script that I'm trying to pull some data from a
I have a php script that can take anything from 2 to 10 minutes
I have an internal proxy that fetches data from my own server and displays
I have php script that must be runned from console (php-cli). But configuration of
I have a PHP script that runs as a CGI program and the HTTP
I have a PHP script that needs to determine if it's been executed via
I have a PHP script that initialises an image gallery. It loops through all
I have a PHP script that can encode a PNG image to a Base64

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.