move player to relevant round in loser bracket using php

I am building double elimination system when match completes in winner bracket the loser is transferred to loser bracket but I am having a problem how to figure out in which round that player needs to be moved i am really depressed as I am unable to figure out I have created a function where it checks on how many slots are available in the current round

if 4 slots available in round 2 8 players can only be accommodated and in round 3 if 2 lots are available and 6 players are available the system will accommodate winners from previous round of loser bracket and move the remaining players which are losers from winner bracket to the next round and if in next round slot is available

The problem is I am not sure how do I get the round number I have created a function but still confused

Function to check the slots in each round and players expected

public function calculate_loser_bracket_rounds($tournamentID = 76, $matchRound = 2) {
    $winnerBracketData = $this->process_data->get_bracket($tournamentID);
    $tournamentData = $this->process_data->get_data('tournament', array('id' => $tournamentID));
    $totalRounds  = count($this->process_data->get_bracket($tournamentID));
    $looserBracketRounds = $totalRounds + 1;
    $bracket      = array();
    $key          = 0;
    $winnerBracketData = $this->process_data->get_bracket($tournamentID);
    $numLoosers = 0;
    $reservedPlayers = 0;

    unset($winnerBracketData[$totalRounds]);

    for($round = 1; $round <= $looserBracketRounds; $round++) {                
        foreach($winnerBracketData as $winnerBracket):
            if($round == $winnerBracket['round']) {
                $numLosers = count($winnerBracket['bracketData']);
                break;
            } else {
                $numLosers = 0;
            }       
        endforeach;

        $divideCurrentSlots = false;

        if ($round == 1) {
            $emptySlots = ($numLosers / 2);
            $winnersForNextRound = $emptySlots;
            $previousRoundMatchesCount[$round] = $emptySlots;

            $totalSlots = $emptySlots;
        } else {
            //First create slots of winners from previous round
            //First get no of winners from previous round 
            $loserBracketWinners = $winnersForNextRound;
            $winnerbracketLosers = $numLosers;
            $reservePlayersPrevRound = $reservedPlayers;

            //Create Matches From Loser bracket winners
            $winnerSlots = $loserBracketWinners / 2;
            $loserSlots  = $winnerbracketLosers / 2;
            $totalSlots  = $winnerSlots + $loserSlots;

            echo 'Round : ' . $round . ' ) <br />';
            
            $slotsForThisRound = $previousRoundMatchesCount[$round - 1];

            if(isset($previousRoundMatchesCount[$round - 1]) && isset($previousRoundMatchesCount[$round - 2])) {
                $previousRoundSlots    = $previousRoundMatchesCount[$round - 1];
                $previoustwoRoundSlots = $previousRoundMatchesCount[$round - 2];

                if($previousRoundSlots == $previoustwoRoundSlots) {
                    $slotsForThisRound = $previoustwoRoundSlots / 2;
                }
            }

            $totalStandingPlayers = ($loserBracketWinners + $winnerbracketLosers + $reservedPlayers);
            echo "Total Players Standing In This Round : " . $totalStandingPlayers . '<br />';
            
            $returnData[$round]['availablePlayers'] = $totalStandingPlayers;

            if($totalSlots != $slotsForThisRound) {
                $reserveSlotForNextRound = $totalSlots - $slotsForThisRound;
                $totalSlots = $totalSlots - $reserveSlotForNextRound;  
                $reservedPlayers = $numLosers;
            }

            $usedPlayersWinnerBracketCurrentRound = ($totalSlots * 2) - $loserBracketWinners - $reservePlayersPrevRound;

            echo "Slots For This Round : " . $totalSlots . '<br />'; 
            echo "Required Players For This Round : " . ($totalSlots * 2) . '<br />';
            echo "Reserved Players From Previous Round : " . $reservePlayersPrevRound . '<br />';
            echo "Reserved Players For Next Round : " . $reservedPlayers . '<br />';
            echo "Players Used From Previous Round (Loser Bracket Winner) : " . $loserBracketWinners . '<br />';
            echo "Players Used From current Round (Winner Bracket Loser) : " . $usedPlayersWinnerBracketCurrentRound . '<br />';
            echo "Losers From winner bracket : " . $winnerbracketLosers . '<br />';
            echo "Winners From Previous round : " . $loserBracketWinners . '<br />';
            echo "<hr />";

            $returnData[$round]['round'] = $round;
            $returnData[$round]['slots'] = $totalSlots;
            $returnData[$round]['reqPlayersCount'] = ($totalSlots * 2);
            $returnData[$round]['pushedNextRound'] = $reservedPlayers;
            $returnData[$round]['playersUsedCurrentRound'] = ($loserBracketWinners + $reservePlayersPrevRound) - ($totalSlots * 2);

            $previousRoundMatchesCount[$round] = $totalSlots;

            $winnersForNextRound = $totalSlots;             
        }

        $numLosers = 0;
        $key++;
    }   

    return $roundToReturn;
}

Now when a match ends there is a function to create new match and save in the database I need to add based on which round that player needs to be moved in.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật