簡體   English   中英

動態獲取/排序下一個多維數組元素

[英]Get/sort next multi-dimensional array element dynamically

我目前正在經歷一個偉大的老腦屁並且動態地選擇下一輪的獲勝者將進入的下一個“圓形比賽”:

生成的梯子


上面的梯子是動態生成的,我想要做的就是找出下一個匹配ID。 我目前已將此作為POC,但如果競爭階梯達到64 /更多,則不可持續:

$ar = [
 1 => [
     ['id' => 1,'name' => 'round1, pair 1'],
     ['id' => 2,'name' => 'round1, pair 2'],
     ['id' => 3,'name' => 'round1, pair 3'],
     ['id' => 4,'name' => 'round1, pair 4'],
 ],
 2 => [
     ['id' => 5,'name' => 'round2, pair 1'],
     ['id' => 6,'name' => 'round2, pair 2'],
 ]
];

$cases = [0, 0, 1, 1, 2, 2];

foreach($ar as $i => $round) {

    foreach($round as $_i => $r) {
        echo $r['name'] . " & NEXT_MATCH_ID::> " . $ar[($i + 1)][$cases[$_i]]['id'] . "<br /> ";
    }
}

例如,有沒有更簡化的方法來實現上述沒有硬編碼變量( $cases )的內容。

基本上,“匹配/對”的數量減半,因為梯子將是: 4 - > 2 - > 1

以上產生了正確的ID,但它不是可擴展的或動態的;

round1, pair 1 & NEXT_MATCH_ID::> 5
round1, pair 2 & NEXT_MATCH_ID::> 5
round1, pair 3 & NEXT_MATCH_ID::> 6
round1, pair 4 & NEXT_MATCH_ID::> 6
round2, pair 1 & NEXT_MATCH_ID::> ...
round2, pair 2 & NEXT_MATCH_ID::> ...
//......etc etc...

如果需要, 演示/上述代碼的示例


筆記

  • “球員/球隊”比賽沒有限制,這可以是指數4, 6, 8, 10, 12, 14, 16, 18....32, 34...64...etc
  • 這將不會發生/適用於最后一輪(總決賽 - 1場比賽),因為沒有進一步的進展。 容易受if($i == count($rounds)) {.... do not continue... )的限制。
  • 有可能同時運行多個匹配,因此“下一輪ID” 不能lastId + 1

只是數學

請記住,每一輪都包含戰隊pow(2, Rounds - Round + 1)和戰隊pow(2, Rounds - Round)比賽。 只需將其作為幾何級數進行求和。

圓形$round前的匹配數是幾何級數 2^(rounds-1) + 2^(rounds-2) + ... 2^(rounds - round + 1) a=2^(rounds-1)r=1/2n=round-1 它的總和是2^(rounds) - 2^(rounds+1-round)

所以匹配id和下一個匹配id只是三個參數的函數: pairnumroundrounds 我將其計算移動到函數getMatchIdgetNextId

<?php
// just matchesInPreviousRounds + parnum
function getMatchId($pairnum, $round, $rounds) {
    // matchesInPreviousRounds - is a sum of a geometric progression  
    // 2^(rounds-1) + 2^(rounds-2) + ... 2^(rounds - round + 1) 
    // with a=2^(rounds-1), r=1/2, n = round-1
    // its sum is 2^(rounds) - 2^(rounds+1-round)
    $inPreviousRounds = $round > 1 ?  (pow(2, $rounds) - pow(2, $rounds + 1 - $round)) : 0;

    $id = $inPreviousRounds + $pairnum;

    return (int)$id;
}

// next id is last id of a round + half a pairnum.
function getNextId($pairnum, $round, $rounds) {
    if($round === $rounds) {
        return false;
    }

    $matchesInThisAndPreviousRounds = pow(2, $rounds) - pow(2, $rounds - $round);

    $nextid = $matchesInThisAndPreviousRounds + ceil($pairnum / 2);

    return (int)$nextid;
}

$divide = 64; // for 1/64 at the start
$power = round(log($divide) / log(2)); // get 6 for 64
$rounds = (int) $power + 1;


for($round = 1; $round <= $rounds; $round++) {
    // every round contains 2^($rounds - $round + 1) of teams 
    // and has 2^($rounds - $round) of matches
    $teamsLeft = pow(2, $rounds - $round + 1);
    $pairsLeft = pow(2, $rounds - $round);


    for($pairnum = 1; $pairnum <= $pairsLeft; $pairnum++) {
        $id = getMatchId($pairnum, $round, $rounds);
        $nextid = getNextId($pairnum, $round, $rounds);

        echo "Round $round, pair $pairnum, id $id ";
        echo "winner goes to " . $nextid ? $nextid : "A BAR" . "\n";
    }
}

其結果

Round 1, pair 1, id 1, winner goes to 65
Round 1, pair 2, id 2, winner goes to 65
...
Round 1, pair 62, id 62, winner goes to 95
Round 1, pair 63, id 63, winner goes to 96
Round 1, pair 64, id 64, winner goes to 96
Round 2, pair 1, id 65, winner goes to 97
Round 2, pair 2, id 66, winner goes to 97
...
Round 2, pair 29, id 93, winner goes to 111
Round 2, pair 30, id 94, winner goes to 111
Round 2, pair 31, id 95, winner goes to 112
Round 2, pair 32, id 96, winner goes to 112
Round 3, pair 1, id 97, winner goes to 113
Round 3, pair 2, id 98, winner goes to 113
...
Round 3, pair 13, id 109, winner goes to 119
Round 3, pair 14, id 110, winner goes to 119
Round 3, pair 15, id 111, winner goes to 120
Round 3, pair 16, id 112, winner goes to 120
Round 4, pair 1, id 113, winner goes to 121
Round 4, pair 2, id 114, winner goes to 121
...
Round 4, pair 7, id 119, winner goes to 124
Round 4, pair 8, id 120, winner goes to 124
Round 5, pair 1, id 121, winner goes to 125
Round 5, pair 2, id 122, winner goes to 125
Round 5, pair 3, id 123, winner goes to 126
Round 5, pair 4, id 124, winner goes to 126
Round 6, pair 1, id 125, winner goes to 127
Round 6, pair 2, id 126, winner goes to 127
Round 7, pair 1, id 127, winner goes to A BAR

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM