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 ) ) {
array_push ( $data["php_resultSet"], $row );
}
asort($someData);
foreach ( $someData as $key) {
array_push ( $data["otherData"], $key );
}
return json_encode($data);