1

我对 PHP 中的 MySQL 数据和数组有一个特殊的问题。

背景:该系统是用于从调查系统导出数据的报告工具。用户可以多次回复任何调查。调查可以有任意数量的问题。

每组响应由一个 column 标识unique_uid,因此 10 个问题的 10 行都具有相同unique_uid的(18-24 位字符串)。每个响应都存储为 table 中的一行scores。用户可能在 table 中有一个帐户accounts,或者他们可能在未注册的情况下以访客身份完成了调查。

我有两个查询来提取:

unique_uid(第一) s 和帐户的DISTINCT 集合name_firstname_last(如果它们存在)。如果用户在没有帐户的情况下响应,则他们是NULL. 结果按 ASC on 排序name_last,然后 ASC on排序name_firstNULL首先订购所有客人 ( ),然后是所有有姓名的帐户。

(其次)一个查询以收集对调查的所有回复,然后将它们与他们的所有者配对。

问题:由于某种原因,我的数组数据没有按照 MySQL 返回它的顺序。我有两个或三个帐户,name_first并且name_last这些帐户散布在NULL帐户之间。

鉴于我在 name_last 然后 name_first 上订购了升序,并且数据按该顺序从 MySQL 返回,并且 PHP 数组按该顺序生成,我无法找出它丢失顺序的原因。PHP是否会根据最后触摸的键重新构造数组数据?

我想也许我的关联键被重铸为索引,所以我在每个 前面加上了字母“a” unique_uid,但这并没有帮助。

感谢您的任何见解,谢谢...

* *编辑:我能够使用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" );
4

1 回答 1

2

This could be because of the length of your keys.

What you can do is either make the keys the same length, say prepend zeros

// 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); 
于 2012-10-18T22:00:11.423 回答