How to sort the associative array based on the specific key?
<p>I tried lot of tricks and googling to solve this problem but couldn't get the useful stuff. </p>

<p>Basically following is the content of an element from an associative array(exam result of candidates and I've to give ranking to the candidates) which contains several such elements. </p>

<p>I want to sort the array in ascending order depending on the key <code>[total_obtained_marks]</code> i.e. candidate having more marks will be first ranked, </p>

<p>then if same values find I've to sort the array in descending order based on key <code>[no_wrong_answers]</code> i.e. less no. of wrong answers. </p>

<p>Even if the rank remains same I've to sort the array in ascending order of the key <code>[test_user_time_used]</code> i.e. the candidate with less time used will get higher rank. </p>

<p>Even then the ranks are same then I've to sort the array on the key <code>[user_first_name]</code> in ascending order i.e alphabetically. </p>

<p>After doing all this stuff I could get the well sorted array which will have proper ranking for candidates. </p>

<p>One more thing about this unsorted array is no actual data could be sorted from the database, so database functions can't be used. </p>

<p>Whatever has to be done will be on this unsorted array itself. Please help me out to solve this issue. Thanks in Advance. Name of the associative array is <code>$test_result</code>. </p>

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

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

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

                    [75069] =&gt; Array
                        (
                            [question_text] =&gt; 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 :&lt;br&gt;
&lt;img alt="\&amp;quot;\&amp;quot;" src="\&amp;quot;http://www.entranceprime.com/upload_media/questions/original/1338456478_10.jpg\&amp;quot;" height="\&amp;quot;169\&amp;quot;" width="\&amp;quot;228\&amp;quot;"&gt;
                            [question_file] =&gt; 
                            [correct_ans_text] =&gt; 25
                            [correct_ans_file] =&gt; 
                            [correct_ans_id] =&gt; 299192
                            [user_marked_ans_text] =&gt; 25
                            [user_marked_ans_file] =&gt; 
                            [user_marked_ans_id] =&gt; 299192
                        )

                    [77526] =&gt; Array
                        (
                            [question_text] =&gt; 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] =&gt; 
                            [correct_ans_text] =&gt; W
                            [correct_ans_file] =&gt; 
                            [correct_ans_id] =&gt; 578079
                            [user_marked_ans_text] =&gt; W
                            [user_marked_ans_file] =&gt; 
                            [user_marked_ans_id] =&gt; 578079
                        )

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

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

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

                    [75631] =&gt; Array
                        (
                            [question_text] =&gt; 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] =&gt; 
                            [correct_ans_text] =&gt; V
                            [correct_ans_file] =&gt; 
                            [correct_ans_id] =&gt; 301488
                            [user_marked_ans_text] =&gt; 
                            [user_marked_ans_file] =&gt; 
                            [user_marked_ans_id] =&gt; 
                        )

                )

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

                )

            [test_result_data] =&gt; Array
                (
                    [no_correct_answers] =&gt; 6
                    [no_wrong_answers] =&gt; 1
                    [no_skipped_questions] =&gt; 38
                    [total_obtained_marks] =&gt; 23
                )

        )
    )
</code></pre>
