在PHP中对多维数组的值进行排名或定位

时间:2015-02-16 02:38:59

标签: php arrays multidimensional-array higher-rank-types

我想根据点对这个跟随数组进行排名,并且重复点将相同排名。不知道我将如何完成这项任务。

Array
(
[6] => Array
    (
        [points] => 0
        [details] => Array
            (
                [name] => name 1
                [rank] => 
            )

    )

[220] => Array
    (
        [points] => 794
        [details] => Array
            (
                [name] => name 2
                [rank] => 
            )

    )

[369] => Array
    (
        [points] => 1002
        [details] => Array
            (
                [name] => name 3
                [rank] => 
            )

    )

[138] => Array
    (
        [points] => 1239
        [details] => Array
            (
                [name] => name 4
                [rank] => 
            )

    )

[778] => Array
    (
        [points] => 1302
        [details] => Array
            (
                [name] => name 5
                [rank] => 
            )

    )
)

1 个答案:

答案 0 :(得分:0)

类似的东西:

$sort=array();
foreach($array as $key=>$v){$sort[$key]=$v['points'];}
sort($sort);
$newArray=array();
$i=0; $points=0;
foreach($sort as $key=>$v){
    $i++;
    if($v['points']>$points){$i++; $points=$v['points'];
    $newArray[$key]=$array['key'];
    $newArray[$key]['rank']=$i;
}
$array=$newArray;