php 用密集秩数填充多维数组的秩列

xoshrz7s  于 2022-12-25  发布在  PHP
关注(0)|答案(3)|浏览(100)

我的数组结构如下-

[
    [
        "points" => 10,
        "details" => ["name" => "Team A", "rank" => ""]
    ],
    [
        "points" => 10,
        "details" => ["name" => "Team B", "rank" => ""]
    ],
    [
        "points" => 8,
        "details" => ["name" => "Team C", "rank" => ""]
    ],
    [
        "points" => 6,
        "details" => ["name" => "Team D", "rank" => ""]
    ],
]

现在我想用合适的密集秩填充数组的“秩”值。

[
    [
        "points" => 10,
        "details" => ["name" => "Team A", "rank" => 1]
    ],
    [
        "points" => 10,
        "details" => ["name" => "Team B", "rank" => 1]
    ],
    [
        "points" => 8,
        "details" => ["name" => "Team C", "rank" => 2]
    ],
    [
        "points" => 6,
        "details" => ["name" => "Team D", "rank" => 3]
    ],
]

我怎样才能得到这样的结果呢?我试着循环遍历数组中的每个元素并比较点,但是我没有发现这真的很有效。

vddsk6oq

vddsk6oq1#

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

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

ttp71kqs2#

有点蛮力,但应该可以。

$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);
5sxhfpxr

5sxhfpxr3#

由于多维输入数组已经按points列降序排序,因此可以循环访问该数组并通过引用修改其rank数据,同时有条件地递增秩变量。
代码:(Demo

$denseRank = 0;
foreach ($array as ['points'=> $points, 'details' => ['rank' => &$rank]]) {
    $denseRanks[$points] ??= ++$denseRank;
    $rank = $denseRanks[$points];
}
var_export($array);

上面的代码片段在foreach()中使用了"数组反结构化"来只声明使用过的变量;声明&$row然后在循环体中访问其元素也是可行的。

相关问题