【发布时间】:2011-09-26 23:22:28
【问题描述】:
我有一个从数据库中获取数据的 php 脚本,结果集以特定顺序返回,例如
Name: John, Age: 21, Address: 1234 Fifth Ave.
我在调试时验证了顺序。然后将结果集推入一个数组,然后将其编码为 json 对象并通过 AJAX 请求传递给我的 jquery 脚本。
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.
}
});
但是,当我取回数据并分析数组时,它会被排序,例如
Address: 1234 Fifth Ave., Age: 21, Name: John Age: 21
我想保留原始结果集的顺序,有没有我必须设置的选项才能做到这一点?感谢您的帮助。
更新:
这是构建数组并对其进行编码的 PHP 代码的 sn-p
$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.
【问题讨论】:
标签: javascript jquery