frosty frosty - 4 years ago 103
PHP Question

Sorting a multidimensional array, and displaying the values from highest to lowerest

I need to sort a multidimensional array, and display the values according to the highest to the lowest. How would I do that?

$array[0][0] = "section1";
$array[0][1] = 3;
$array[1][0] = "section2";
$array[1][1] = 1;
$array[2][0] = "section3";
$array[2][1] = 2;

echo "<pre>";
print_r($array);
echo "</pre>";


Result:

section1
3
section2
1
section3
2


Need Result:

section1
3
section3
2
section2
1

Answer Source

See usort

function cmp($a, $b) {
        return $b[1] - $a[1];
}
usort($array, "cmp");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download