【问题标题】:How can i set default values to array before results is added如何在添加结果之前将默认值设置为数组
【发布时间】:2021-05-14 03:30:31
【问题描述】:

我有排名方法,所有来自联赛的数据都在哪里。 例如。

第一联赛有 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,
    ]);


}

【问题讨论】:

  • 你面临的具体问题是什么
  • @MKhalidJunaid,问题是如果球队还没有踢过比赛,那么它不会显示在积分榜数组中

标签: arrays laravel model-view-controller multidimensional-array eloquent


【解决方案1】:

我想你可以先用$blank 为所有团队填充你的$standings 集合

$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 中的所有球队,无论他们的比赛如何,稍后您的下一个循环将填写参加过比赛的球队的相关信息

【讨论】:

    猜你喜欢
    • 2018-08-29
    • 2017-05-15
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多