1

PHP の MySQL データと配列に特有の問題があります。

背景:このシステムは、調査システムからデータをエクスポートするために使用されるレポート ツールです。ユーザーは任意のアンケートに何度でも回答できます。調査には、任意の数の質問を含めることができます。

回答の各セットは列によって識別されるunique_uidため、10 個の質問に対する 10 行はすべて同じになりますunique_uid(18 ~ 24 桁の文字列)。各応答は、 table に行として格納されますscores。ユーザーは表 のアカウントを持っているaccountsか、登録せずにゲストとしてアンケートに回答した可能性があります。

抽出するために構築された2つのクエリがあります。

unique_uid(最初)とアカウントのname_firstand name_last(存在する場合) の DISTINCT コレクション。ユーザーがアカウントなしで応答した場合、それらはNULL. 結果は ASC on name_last、次に ASC on の順に並べられname_firstます。すべてのゲスト ( NULL) が最初に並べられ、その後に名前を持つすべてのアカウントが続きます。

(2 つ目) 調査へのすべての回答を収集し、それらを所有者とペアにするクエリ。

問題:何らかの理由で、MySQL が返す順序で配列データが維持されません。私は 2 つまたは 3 つのアカウントを持ってname_firstおりname_last、アカウント間に散在していNULLます。

name_last、name_first の順に昇順で注文し、MySQL からその順序でデータが返され、PHP 配列がその順序で生成されることを考えると、順序が失われている理由がわかりません。PHP は、最後に触れたキーに基づいて配列データを再構築しますか?

おそらく連想キーがインデックス化されたものとして再キャストされているのではないかと考えたので、それぞれの先頭に文字「a」を追加しましunique_uidたが、それは役に立ちませんでした。

あなたが持っているかもしれない洞察に感謝します、ありがとう...

* *EDIT : (この投稿の下部にあるコード)を使用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"
  }
[..]

2 番目のクエリのサンプル データ:

  [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 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 に答える