PHP排名系统数组排序

时间:2009-10-07 23:25:41

标签: php sorting ranking

我开发了一个排名系统。我得到了以下数组:

[1] => Array
    (
        [botarin - Branding und Kommunikation] => 1
        [Raiffeisen Kredit 2 Go] => 2
    )

[2] => Array
    (
        [Kindersteckdosen] => 1
        [Surf lieber mit bob] => 1
        [Lafarge Imageinserate] => 1
        [MCG Messecongress Graz Inserate] => 1
    )

1,2是类别id,然后是项目名称和每个项目的投票数量。我如何对数组进行排序,因此类别ID保持这样排序,但项目名称按投票数下降排名?

任何想法?

提前感谢!

3 个答案:

答案 0 :(得分:3)

// $full_array is your array of category ID's with projects/votes as nested arrays

foreach ($full_array as $cat_id => $projects) {
    asort($projects, SORT_NUMERIC);
    $full_array[$cat_id] = $projects;
}

// Each category ID  within $full_array is now sorted

答案 1 :(得分:0)

迭代数组。对于每个子数组,请使用asort

答案 2 :(得分:0)

要按降序排序,请使用arsort();

$a = array( "a" => 2 );
arsort( $a );
print_r( $a );