Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>PHP provides <a href="http://php.net/manual/en/array.sorting.php" rel="nofollow">quite a few functions for sorting arrays</a>. In this case, the one you want is <a href="http://php.net/uasort" rel="nofollow"><code>uasort()</code></a>, which sorts an array according to a user-defined comparison function while maintaining key-value associations:</p> <pre><code>function compare_results ( $a, $b ) { $diff = strcmp( $a['test_user_data']['user_status'], $b['test_user_data']['user_status'] ); if ( $diff != 0 ) return $diff &lt; 0 ? -1 : 1; // ascending order // ("Attempted" &lt; "Not attempted") $diff = $a['test_result_data']['total_obtained_marks'] - $b['test_result_data']['total_obtained_marks']; if ( $diff != 0 ) return $diff &gt; 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 &lt; 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 &lt; 0 ? -1 : 1; // ascending order $diff = strcmp( $a['test_user_data']['user_name'], $b['test_user_data']['user_name'] ); if ( $diff != 0 ) return $diff &lt; 0 ? -1 : 1; // ascending order return 0; // give up, they're as good as equal } uasort( $test_result, compare_results ); </code></pre> <p>Writing such comparison functions is (briefly) documented on the <a href="http://php.net/uasort" rel="nofollow"><code>usort()</code></a> function documentation page.</p> <p>Note, in particular, the warning about non-integer return values being truncated, which is why I'm returning <code>$diff &lt; 0 ? -1 : 1</code> rather than just <code>$diff</code> itself. In your case, this <em>shouldn't</em> matter, since the numeric values appear to be integers, but it could cause hard-to-find bugs if the system was ever modified to allow, say, partial marks to be awarded. Thus, always returning either -1, 0 or 1 from comparison functions is a good habit to get into.</p> <p>(For the string comparisons, the <code>$diff &lt; 0 ? -1 : 1</code> is doubly pointless, since <a href="http://php.net/strcmp" rel="nofollow"><code>strcmp()</code></a> is documented to always return -1, 0 or 1 anyway, but I've used it there also for the sake of consistency. It doesn't really do any harm, and as I said, it's a good habit to get into.)</p> <p>Also note that I'm comparing the time values as strings. If this does not yield the results you're expecting, you'll need to write your own time comparison code.</p> <p>Ps. If you're using PHP 5.3.0 or later, you can also write the comparison function as an <a href="http://php.net/manual/en/functions.anonymous.php" rel="nofollow">anonymous function</a>, like this:</p> <pre><code>uasort( $test_result, function ( $a, $b ) { // function body goes here } ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload