如何基于包含的关联数组在php中对数组进行排序?

时间:2011-02-09 22:33:26

标签: php sorting

我有一组关联数组:

a[0] = array("value" => "fred", "score" => 10);
a[1] = array("value" => "john", "score" => 50);
a[2] = array("value" => "paul", "score" => 5);

我想根据相关数组的“得分”值对“a”数组进行排序

它会变成:

a[0] = array("value" => "paul", "score" => 5);
a[1] = array("value" => "fred", "score" => 10);
a[2] = array("value" => "john", "score" => 50);

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:4)

您需要使用usort和比较功能。

类似的东西:

function cmp($a, $b)
{
    if ($a['score'] == $b['score']) {
        return 0;
    }
    return ($a['score'] < $b['score']) ? -1 : 1;
}