我在PHP中遇到MySQL数据和数组的特殊问题。
背景:系统是一种用于从调查系统导出数据的报告工具。用户可以多次回复任何调查。调查可以有任意数量的问题。
每组回复都由列unique_uid
标识,因此10个问题的10行都具有相同的unique_uid
(18-24位数字符串)。每个响应都存储在表scores
中的一行中。用户可能在表accounts
中拥有帐户,或者他们可能已作为访客完成了调查而未进行注册。
我有两个问题需要提取:
(首先)DISTINCT unique_uid
s集合以及帐户name_first
和name_last
(如果存在)。如果用户没有帐户回复,则为NULL
。结果在name_last
上按ASC排序,然后在name_first
上按ASC排序。首先订购所有访客(NULL
),然后是所有名称的帐户。
(其次)查询收集调查的所有回复,然后将其与所有者配对。
问题:由于某种原因,我的数组数据不是按照MySQL返回的顺序。我有两个或三个name_first
和name_last
帐户,这些帐户散布在NULL
个帐户中。
鉴于我在name_last上命令提升,然后命名为name_first,并且数据按顺序从MySQL返回,并且PHP数组按此顺序生成,我无法找出它失败的原因。 PHP是否根据最后触摸的键重新构造数组数据?
我认为也许我的关联键被重新编号为索引,所以我在每个unique_uid
前加上字母“a”,但这没有帮助。
感谢您的任何见解,谢谢......
* *编辑:我能够使用usort()
(本文底部的代码)按顺序获取数据。但仍然无法弄清楚为什么它首先出现故障:(
$rs = $wpdb->get_results( 'SELECT DISTINCT unique_uid, u.name_first, u.name_last FROM wp_fen_cme_scores s LEFT JOIN wp_fen_cme_accounts u ON u.uid = s.account_uid WHERE s.test_uid = ' . $y . ' ORDER BY u.name_last' );
// ** When I var_dump( $rs ), the data is in the correct order **
$userdata = array();
foreach ( $rs as $row ){
$userdata[ 'a' . $row->unique_uid ] = array( $row->name_first, $row->name_last, array() );
}
// ** When I var_dump( $userdata ), the data is no longer in the correct order **
$rs = $wpdb->get_results( 'SELECT s.unique_uid, s.question_uid, s.answer, s.correct, s.time, s.lost_focus_count, s.date_created, (q.correct_answer < 1 ) AS open FROM wp_fen_cme_scores s, wp_fen_cme_questions q WHERE s.test_uid = ' . $y . ' AND q.uid = s.question_uid ORDER BY q.sort ASC');
foreach ( $rs as $row ){
$userdata[ 'a' . $row->unique_uid ][2][ $row->question_uid ] = array( $row->answer, $row->correct, $row->time, $row->lost_focus_count, $row->open, $row->date_created );
}
来自热门查询的一些示例数据:
[..]
[46]=>
object(stdClass)#315 (3) {
["unique_uid"]=>
string(20) "20977191501349809722"
["name_first"]=>
NULL
["name_last"]=>
NULL
}
[47]=>
object(stdClass)#316 (3) {
["unique_uid"]=>
string(19) "6630155101349813205"
["name_first"]=>
NULL
["name_last"]=>
NULL
}
[48]=>
object(stdClass)#317 (3) {
["unique_uid"]=>
string(21) "982542341421349813493"
["name_first"]=>
string(14) "Patrick"
["name_last"]=>
string(15) "Moore"
}
[49]=>
object(stdClass)#318 (3) {
["unique_uid"]=>
string(19) "7589292181349812907"
["name_first"]=>
string(5) "Mallory"
["name_last"]=>
string(9) "Moore"
}
[..]
第二次查询的示例数据:
[0]=>
object(stdClass)#262 (8) {
["unique_uid"]=>
string(20) "16079139101349813111"
["question_uid"]=>
string(2) "41"
["answer"]=>
string(13) "Health Center"
["correct"]=>
string(1) "1"
["time"]=>
string(3) "5.0"
["lost_focus_count"]=>
string(1) "1"
["date_created"]=>
string(19) "2012-10-09 16:05:18"
["open"]=>
string(1) "1"
}
[1]=>
object(stdClass)#261 (8) {
["unique_uid"]=>
string(19) "7491272021349813110"
["question_uid"]=>
string(2) "41"
["answer"]=>
string(28) "Community-Based Organization"
["correct"]=>
string(1) "1"
["time"]=>
string(3) "5.7"
["lost_focus_count"]=>
string(1) "0"
["date_created"]=>
string(19) "2012-10-09 16:05:17"
["open"]=>
string(1) "1"
}
[2]=>
object(stdClass)#260 (8) {
["unique_uid"]=>
string(20) "20879148791349813105"
["question_uid"]=>
string(2) "41"
["answer"]=>
string(13) "Health Center"
["correct"]=>
string(1) "1"
["time"]=>
string(3) "5.3"
["lost_focus_count"]=>
string(1) "1"
["date_created"]=>
string(19) "2012-10-09 16:05:13"
["open"]=>
string(1) "1"
}
[3]=>
object(stdClass)#259 (8) {
["unique_uid"]=>
string(19) "6630155101349813079"
["question_uid"]=>
string(2) "41"
["answer"]=>
string(22) "Other Clinical Setting"
["correct"]=>
string(1) "1"
["time"]=>
string(4) "18.4"
["lost_focus_count"]=>
string(1) "0"
["date_created"]=>
string(19) "2012-10-09 16:04:59"
["open"]=>
string(1) "1"
}
[..]
排序功能:
function custom_sort( $a, $b ){
// Compare on `name_last` (the second item in $userdata array)
return $a[1] > $b[1] ;
}
usort( $userdata, "custom_sort" );
答案 0 :(得分:2)
这可能是因为你的钥匙长度。
你可以做的是让键长度相同,比如说前置零
// This will prepend zeros before the key and keep only the last
// 24 characters. If your key is 24 characters long it will be
// intact, otherwise it will end up with a few zeros at the front
$key = substr('00000' . $row->unique_uid, -24);