PHP获取多维数组中的值的等级

时间:2013-04-07 13:11:49

标签: php arrays multidimensional-array

我的数组结构如下 -

Array
(
    [0] => Array
        (
            [points] => 10
            [details] => Array
                    (
                        [name] => Team A
                        [rank] => 
                    )
        )
    [1] => Array
        (
            [points] => 10
            [details] => Array
                    (
                        [name] => Team B
                        [rank] => 
                    )
        )
    [2] => Array
        (
            [points] => 8
            [details] => Array
                    (
                        [name] => Team C
                        [rank] => 
                    )
        )
    [3] => Array
        (
            [points] => 6
            [details] => Array
                    (
                        [name] => Team D
                        [rank] => 
                    )
        )
)

现在我想要结果数组 -

Array
(
    [0] => Array
        (
            [points] => 10
            [details] => Array
                    (
                        [name] => Team A
                        [rank] => 1
                    )
        )
    [1] => Array
        (
            [points] => 10
            [details] => Array
                    (
                        [name] => Team B
                        [rank] => 1
                    )
        )
    [2] => Array
        (
            [points] => 8
            [details] => Array
                    (
                        [name] => Team C
                        [rank] => 2
                    )
        )
    [3] => Array
        (
            [points] => 6
            [details] => Array
                    (
                        [name] => Team D
                        [rank] => 3
                    )
        )
)

如何实现此输出?我尝试循环遍历数组中的每个元素并比较点,但我没有发现真正的效果。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

如何创建另一个数组并存储所需的结果

$array = array(
    array(
        "points" => 10,
        "details" => array(
            "name" => "Team A",
            "rank" => ""
        )
    ),
    array(
        "points" => 11,
        "details" => array(
            "name" => "Team B",
            "rank" => ""
        )
    )
);

$c = 0; // count identifier
$n = array(); // create new array

for ($i=0;$i<count($array);$i++){ // loop through each array
    foreach ($array[$i] as $value){ // loop through into sub arrays
        if (is_array($value)){
            $n[$i]['details'] = array(
                "name" => $value['name'],
                "rank" => $c
            );
            $c++;
        } else {
            $n[$i]['points'] = $value;
        }
    }
}

print_r($n);

输出将是:

Array ( [0] => Array ( [points] => 10 [details] => Array ( [name] => Team A [rank] => 0 ) ) [1] => Array ( [points] => 11 [details] => Array ( [name] => Team B [rank] => 1 ) ) ) 

答案 1 :(得分:1)

有点暴力但它应该有用。

$array = array(
    array(
    "points" => 10,
    "details" => array(
        "name" => "Team A",
        "rank" => ""
    )
    ),
    array(
    "points" => 11,
    "details" => array(
        "name" => "Team B",
        "rank" => ""
    )
    ),
    array(
    "points" => 10,
    "details" => array(
        "name" => "Team A",
        "rank" => ""
    )
    ),
    array(
    "points" => 11,
    "details" => array(
        "name" => "Team B",
        "rank" => ""
    )
    )
);
$points = array();
foreach($array as $key => $arr){
    $points[] = $arr['points'];
}
asort($points);

foreach($points as $pkey => $point){
    foreach($array as $akey => $arr){
        if($point == $arr['points']){
            $array[$akey]['details']['rank'] = $pkey+1;
        }
    }
}


var_dump($array);