在添加结果之前如何将默认值设置为数组

How can i set default values to array before results is added

我有积分榜方法,联赛比赛的所有数据在哪里。 例如

第一联赛有 10 支球队。并且在添加结果时计算排名。但如果部分队伍没有添加成绩,则不会添加到积分榜中。

主要目标是让所有球队进入积分榜,如果球队没有任何比赛,则为 nvm。 在添加结果之前,排名中的所有结果都应为 0(积分、比赛、胜利、失败和其他)

这是我的排名方法:

public function standings(League $league, Team $team)
{
    $standings = [];

    $blank = [
        'points' => 0,
        'scoredGoals' => 0,
        'goalsConceded' => 0,
        'wins' => 0,
        'loses' => 0,
        'draws' => 0,
    ];

    $matches = Match::with('score', 'homeTeam', 'awayTeam')
    ->whereHas('score', function($query){
        $query->whereNotNull('home_team_score')
            ->whereNotNull('away_team_score');
    })
    ->where('league_id', '=', $league->id)
    ->get();

    foreach ($matches as $match) {

        $homeTeamScore = $match->score->home_team_score;
        $awayTeamScore = $match->score->away_team_score;

        if (! isset($standings[$match->homeTeam->name])) {
            $standings[$match->homeTeam->name] = $blank;
        }

        if (! isset($standings[$match->awayTeam->name])) {
            $standings[$match->awayTeam->name] = $blank;
        }

        $home = &$standings[$match->homeTeam->name];
        $away = &$standings[$match->awayTeam->name];

        $away['scoredGoals'] += $awayTeamScore;
        $home['scoredGoals'] += $homeTeamScore;
        $away['goalsConceded'] += $homeTeamScore;
        $home['goalsConceded'] += $awayTeamScore;
        switch ($homeTeamScore <=> $awayTeamScore) {
            case -1:
                // home lost
                // swap home and away and let it fall through
                $tmpHome = &$home;
                $home = &$away;
                $away = &$tmpHome;
            case 1:
                // home won
                $home['points'] += 3;
                $home['wins']++;
                $away['loses']++;
                break;
            default:
                // draw
                $home['points']++;
                $away['points']++;
                $home['draws']++;
                $away['draws']++;
        }
    }

    $standings = collect($standings)->sort(function ($one, $other) {
        if ($one['points'] !== $other['points']) {
            return $other['points'] - $one['points'];  // similar to desc
        }

        $oneDelta = $one['scoredGoals'] - $one['goalsConceded'];
        $otherDelta = $other['scoredGoals'] - $other['goalsConceded'];

        return $otherDelta - $oneDelta; // similar to desc
    });
    return view('admin.leagues.standings')->with([
        'standings' => $standings,
    ]);


}

我想你可以先为所有团队$blank填写你的$standingscollection项目

$standings = [];

$blank = [
    'points' => 0,
    'scoredGoals' => 0,
    'goalsConceded' => 0,
    'wins' => 0,
    'loses' => 0,
    'draws' => 0,
];
$teams = Team::all();
foreach ($teams as $team) {    
    $standings[$team->name] = $blank;
}

现在您可以省略循环中的后续检查和初始化

if (! isset($standings[$match->homeTeam->name])) {
    $standings[$match->homeTeam->name] = $blank;
}

if (! isset($standings[$match->awayTeam->name])) {
    $standings[$match->awayTeam->name] = $blank;
}

这样你就会得到$standings中的所有球队,而不管他们的比赛,稍后你的下一个循环将填写参加过比赛的球队的相关信息