1

私はこの問題を解決するためにたくさんのトリックとグーグルを試しましたが、有用なものを得ることができませんでした。

基本的に以下は、いくつかのそのような要素を含む連想配列(候補の試験結果と私は候補にランク付けを与える必要があります)からの要素の内容です。

キーに応じて配列を昇順で並べ替えたい、[total_obtained_marks]つまり、マークの多い候補が最初にランク付けされます。

次に、同じ値が見つかった場合は、キーに基づいて降順で配列を並べ替える必要が[no_wrong_answers]あります。間違った答えの。

ランクが同じままであっても、キーの昇順で配列を並べ替える必要があります[test_user_time_used]。つまり、使用時間が短い候補の方がランクが高くなります。

それでもランクは同じなので、キーの配列を[user_first_name]昇順、つまりアルファベット順に並べ替える必要があります。

これらすべてのことを行った後、候補者の適切なランキングを持つ、適切にソートされた配列を取得できました。

このソートされていない配列のもう1つの点は、データベースから実際のデータをソートできないため、データベース関数を使用できないことです。

実行する必要があることはすべて、このソートされていない配列自体になります。この問題を解決するために私を助けてください。前もって感謝します。連想配列の名前はです$test_result

Array
(
    [14ddcea23dfc46bed4f2a15da7901c51] => Array
        (
            [test_user_data] => Array
                (
                    [test_user_id] => 6297
                    [test_user_user_id] => 14ddcea23dfc46bed4f2a15da7901c51
                    [test_user_test_id] => 348
                    [user_first_name] => Shahin
                    [user_last_name] => Khan
                    [user_name] => Shahin Khan
                    [test_user_status] => present
                    [test_user_time_used] => 00:00:00 Hr
                    [test_user_start_time] => 1356514472
                    [test_duration] => 4500
                    [test_name] => NEET: Electrostatics 1
                    [test_mode] => non-schedule
                    [test_end_time] => 0
                    [user_status] => Attempted
                )

            [test_question_data] => Array
                (
                    [73101] => Array
                        (
                            [question_text] => If a glass rod is rubbed with silk it acquires a positive charge because :
                            [question_file] => 
                            [correct_ans_text] => electrons are removed from it
                            [correct_ans_file] => 
                            [correct_ans_id] => 291299
                            [user_marked_ans_text] => electrons are removed from it
                            [user_marked_ans_file] => 
                            [user_marked_ans_id] => 291299
                        )

                    [77101] => Array
                        (
                            [question_text] => In a charged capacitor , the energy is stored in :
                            [question_file] => 
                            [correct_ans_text] => the field between the plates
                            [correct_ans_file] => 
                            [correct_ans_id] => 307468
                            [user_marked_ans_text] => the field between the plates
                            [user_marked_ans_file] => 
                            [user_marked_ans_id] => 307468
                        )

                    [75069] => Array
                        (
                            [question_text] => ABC is right-angle triangle with sides AB = 3 cm, BC = 4 cm, AC = 5 cm, charges 15, 12 and -20 respectively. Magnitude of the force experienced by the charge at B in dynes is :<br>
<img alt="\&quot;\&quot;" src="\&quot;http://www.entranceprime.com/upload_media/questions/original/1338456478_10.jpg\&quot;" height="\&quot;169\&quot;" width="\&quot;228\&quot;">
                            [question_file] => 
                            [correct_ans_text] => 25
                            [correct_ans_file] => 
                            [correct_ans_id] => 299192
                            [user_marked_ans_text] => 25
                            [user_marked_ans_file] => 
                            [user_marked_ans_id] => 299192
                        )

                    [77526] => Array
                        (
                            [question_text] => The work done in increasing the coltage across the plates of a capacitor from 5 V to 10 V is W. The work done in increasing the voltage from 10 V to 15 V will be :
                            [question_file] => 
                            [correct_ans_text] => W
                            [correct_ans_file] => 
                            [correct_ans_id] => 578079
                            [user_marked_ans_text] => W
                            [user_marked_ans_file] => 
                            [user_marked_ans_id] => 578079
                        )

                    [73190] => Array
                        (
                            [question_text] => Select the corrent alternative :
                            [question_file] => 
                            [correct_ans_text] => Charge cannot exist without matter although matter can exist without charge
                            [correct_ans_file] => 
                            [correct_ans_id] => 291651
                            [user_marked_ans_text] => Charge cannot exist without matter although matter can exist without charge
                            [user_marked_ans_file] => 
                            [user_marked_ans_id] => 291651
                        )

                    [127452] => Array
                        (
                            [question_text] => Capacity of a conductor depends upon
                            [question_file] => 
                            [correct_ans_text] => size of conductor <br>
                            [correct_ans_file] => 
                            [correct_ans_id] => 510229
                            [user_marked_ans_text] => all of these<br>
                            [user_marked_ans_file] => 
                            [user_marked_ans_id] => 510232
                        )

                    [127503] => Array
                        (
                            [question_text] => A charged spherical shell does not produce an electric field at any
                            [question_file] => 
                            [correct_ans_text] => interior point <br>
                            [correct_ans_file] => 
                            [correct_ans_id] => 510425
                            [user_marked_ans_text] => interior point <br>
                            [user_marked_ans_file] => 
                            [user_marked_ans_id] => 510425
                        )

                    [75631] => Array
                        (
                            [question_text] => A solid conducting sphere having a charge Q is surrounded by an uncharged concentric conducting hollow spherical shell. Let the potential difference between the surface of the solid sphere and that of the outer surface of the hollow shell be V . If the shell is now given a charge -3q , the new potential difference between the same two surface is :
                            [question_file] => 
                            [correct_ans_text] => V
                            [correct_ans_file] => 
                            [correct_ans_id] => 301488
                            [user_marked_ans_text] => 
                            [user_marked_ans_file] => 
                            [user_marked_ans_id] => 
                        )

                )

            [subject_wise_result] => Array
                (
                    [5] => Array
                        (
                            [subject_id] => 5
                            [subject_name] => 12 PHYSICS
                            [no_correct_answers] => 6
                            [no_wrong_answers] => 1
                            [no_skipped_questions] => 38
                            [total_obtained_marks] => 23
                            [total_questions] => 45
                            [total_percent] => 100 %
                            [gain_percent] => 12.78 % 
                        )

                )

            [test_result_data] => Array
                (
                    [no_correct_answers] => 6
                    [no_wrong_answers] => 1
                    [no_skipped_questions] => 38
                    [total_obtained_marks] => 23
                )

        )
    )
4

2 に答える 2

7

PHPには、配列をソートするための関数が多数用意されています。この場合、必要なのは です。これはuasort()、キーと値の関連付けを維持しながら、ユーザー定義の比較関数に従って配列を並べ替えます。

function compare_results ( $a, $b ) {
    $diff = strcmp( $a['test_user_data']['user_status'],
                    $b['test_user_data']['user_status'] );
    if ( $diff != 0 ) return $diff < 0 ? -1 : 1;  // ascending order
    // ("Attempted" < "Not attempted")

    $diff = $a['test_result_data']['total_obtained_marks'] -
            $b['test_result_data']['total_obtained_marks'];
    if ( $diff != 0 ) return $diff > 0 ? -1 : 1;  // descending order

    $diff = $a['test_result_data']['no_wrong_answers'] -
            $b['test_result_data']['no_wrong_answers'];
    if ( $diff != 0 ) return $diff < 0 ? -1 : 1;  // ascending order

    $diff = strcmp( $a['test_user_data']['test_user_time_used'],
                    $b['test_user_data']['test_user_time_used'] );
    if ( $diff != 0 ) return $diff < 0 ? -1 : 1;  // ascending order

    $diff = strcmp( $a['test_user_data']['user_name'],
                    $b['test_user_data']['user_name'] );
    if ( $diff != 0 ) return $diff < 0 ? -1 : 1;  // ascending order

    return 0;  // give up, they're as good as equal
}

uasort( $test_result, compare_results );

このような比較関数の作成については、usort()関数のドキュメント ページに (簡単に) 記載されています。

特に、整数以外の戻り値が切り捨てられるという警告に注意してください。そのため、それ自体$diff < 0 ? -1 : 1ではなく戻り$diffます。あなたの場合、数値は整数のように見えるため、これは問題ではありませんが、システムが変更されて、たとえば部分的なマークが付与されるように変更された場合、見つけにくいバグが発生する可能性があります。したがって、比較関数から常に -1、0、または 1 のいずれかを返すことは、習慣化するのに適しています。

(文字列比較の場合、$diff < 0 ? -1 : 1は常に -1、0、または 1 を返すことが文書化されているため、二重に無意味strcmp()ですが、一貫性のためにも使用しました。実際には害はありません。私は言いました、それは良い習慣です。)

また、時間値を文字列として比較していることにも注意してください。これで期待した結果が得られない場合は、独自の時間比較コードを作成する必要があります。

Ps。PHP 5.3.0 以降を使用している場合は、次のように比較関数を無名関数として記述することもできます。

uasort( $test_result, function ( $a, $b ) {
    // function body goes here
} );
于 2013-01-09T16:42:30.470 に答える
0

独自の比較関数を定義し、を使用して配列を並べ替える必要がありますusort()

function cmp($a, $b) {
    $a = $a['test_result_data']['total_obtained_marks'];
    $b = $b['test_result_data']['total_obtained_marks'];
    if ($a == $b) return 0;
    return ($a < $b ? -1 : 1);
}

usort($array, 'cmp');
于 2013-01-09T16:14:30.610 に答える