You actually need 2 sorts here. The 1st one sorts each subarray of 3 entries descending, so the highest is always in entry 0.
The second sort is a multi-sort that sorts the main entry descending on the basis of the subarray entry 0 (highest value in subarray).
The result set shows each entry in the main array, its order based on the highest value anywhere in the sub-array. The result set keeps the key of the main array intact.
Try the following code, assuming the array name $a. The multisort code snippet thanks to
remmy.cjb.net in the php documentation at
uasort.
[php]<?php
// The default sort order is ascending, and the default sort
// type is strnatcmp.
// function multisort($array[, $key, $order, $type]...)
function multisort($array) {
for($i = 1; $i < func_num_args(); $i += 3) {
$key = func_get_arg($i);
$order = true;
if($i + 1 < func_num_args())
$order = func_get_arg($i + 1);
$type = 0;
if($i + 2 < func_num_args())
$type = func_get_arg($i + 2);
switch($type) {
case 1: // Case insensitive natural.
$t = 'strcasenatcmp($a[' . $key . '], $b[' . $key . '])';
break;
case 2: // Numeric.
$t = '$a[' . $key . '] - $b[' . $key . ']';
break;
case 3: // Case sensitive string.
$t = 'strcmp($a[' . $key . '], $b[' . $key . '])';
break;
case 4: // Case insensitive string.
$t = 'strcasecmp($a[' . $key . '], $b[' . $key . '])';
break;
default: // Case sensitive natural.
$t = 'strnatcmp($a[' . $key . '], $b[' . $key . '])';
break;
}
uasort($array, create_function('$a, $b', 'return ' . ($order ? '' : '-') . '(' . $t . ');'));
}
return $array;
}
echo '<br><pre>PRE-SORT:<br>'; print_r($a);
foreach ($a as $key => $val) {
rsort($a[$key]);
}
echo '<br><pre>SORT 1:<br>'; print_r($a);
// This works like MYSQL 'ORDER BY id DESC, name ASC'
// Note the quoting of string literal keys.
echo '<br><pre>SORT 2:<br>';
print_r(multisort($a, "0", false, 0));
echo('</pre>');
?> [/php]
Ronald :cool: