Jquery ajax returns sorted data

I have a php script that retrieves data from a database, the result set is returned in a specific order, for example.

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

I checked the order during debugging. Then the result set is put into an array, which is then encoded as a json object and passed to my jquery script via 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 return the data and parse the array, it is sorted, for example.

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

I want to keep the order of the original result set, is there an option I have to set for this? Help is appreciated.

UPDATE:

Here is a snippet of PHP code that builds an 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.
+2
source share
3 answers

( ) JSON / - , . , Perl , .

, , , JavaScript AJAX, , :

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

, .

, , .

, JSON "aa_" , "zz_". , - "" "", "aa_Name" "ab_Address".

, fieldName=fieldName.substring(3).

+1

, , Name: John, Age: 21, Address: 1234 Fifth Ave. JSON , (http://php.net/manual/en/function.json-encode.php) ( ) for in, (https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in). , php-.

+2

, - , . JSON . , , " " , , DB (), JSON.

0

All Articles