如何根据特定键对关联数组进行排序?

时间:2013-01-09 15:52:10

标签: php

我尝试了许多技巧和谷歌搜索来解决这个问题,但无法获得有用的东西。

基本上,以下是来自关联数组的元素的内容(候选人的考试结果和我要给候选人排名),其中包含几个这样的元素。

我希望根据键[total_obtained_marks]按升序对数组进行排序,即具有更多标记的候选者将首先排名,

然后,如果相同的值发现我要根据键[no_wrong_answers]按降序对数组进行排序,即少于no。错误的答案。

即使排名保持不变,我也要按照密钥[test_user_time_used]的升序对数组进行排序,即使用较少时间的候选人将获得更高的排名。

即使这样,等级也是相同的,然后我按字母顺序按升序排列键[user_first_name]上的数组。

完成所有这些工作后,我可以获得排序良好的数组,这将对候选人有适当的排名。

关于这个未排序数组的另一件事是没有实际数据可以从数据库中排序,因此不能使用数据库函数。

无论做什么都将在这个未排序的阵列上。请帮我解决这个问题。提前致谢。关联数组的名称是$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
                )

        )
    )

2 个答案:

答案 0 :(得分:7)

PHP提供quite a few functions for sorting arrays。在这种情况下,您需要的是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是毫无意义的,因为strcmp()被记录为总是返回-1,0或1,但我也在那里使用它也是为了一致性。它并没有真正造成任何伤害,正如我所说,这是一个很好的习惯。)

另请注意,我将时间值作为字符串进行比较。如果这不能产生您期望的结果,则需要编写自己的时间比较代码。

聚苯乙烯。如果您使用的是PHP 5.3.0或更高版本,您还可以将比较函数编写为anonymous function,如下所示:

uasort( $test_result, function ( $a, $b ) {
    // function body goes here
} );

答案 1 :(得分: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');