0

我的数组结构如下 -

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
                    )
        )
)

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

4

2 回答 2

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);
于 2013-04-07T14:14:33.650 回答
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 ) ) ) 
于 2013-04-07T13:45:09.943 回答