Arrays 从数据库中获取阵列中的其他数据

Arrays 从数据库中获取阵列中的其他数据,arrays,laravel,model-view-controller,multidimensional-array,eloquent,Arrays,Laravel,Model View Controller,Multidimensional Array,Eloquent,我有球队信息和计算分数的排名数组。但我也需要计算每支球队的进球数。 需要如何将其提取到当前阵列的帮助 这是我的联盟管理员: public function standings(League $league, Team $team) { $standings = [ ]; $matches = Match::where('league_id', '=', $league->id)->get(); foreach($matches

我有球队信息和计算分数的排名数组。但我也需要计算每支球队的进球数。 需要如何将其提取到当前阵列的帮助

这是我的联盟管理员:

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

    $matches = Match::where('league_id', '=', $league->id)->get();

    foreach($matches as $match) {
        $homeTeamScore = $match->score->home_team_score;
        $awayTeamScore = $match->score->away_team_score;

        if ($homeTeamScore === $awayTeamScore) {
            if (isset($standings[$match->homeTeam->name])) {
                $standings[$match->homeTeam->name] += 1;
            } else {
                $standings[$match->homeTeam->name] = 1;
            }

            if (isset($standings[$match->awayTeam->name])) {
                $standings[$match->awayTeam->name] += 1;
            } else {
                $standings[$match->awayTeam->name] = 1;
            }
        }

        if ($homeTeamScore > $awayTeamScore) {
            if (isset($standings[$match->homeTeam->name])) {
                $standings[$match->homeTeam->name] += 3;
            } else {
                $standings[$match->homeTeam->name] = 3;
            }

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

        if ($homeTeamScore < $awayTeamScore) {
            if (isset($standings[$match->awayTeam->name])) {
                $standings[$match->awayTeam->name] += 3;
            } else {
                $standings[$match->awayTeam->name] = 3;
            }

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

    return view('admin.leagues.standings')->with('standings',$standings);
}
我想这样做:

array:3 [▼
  "firstTeam" => array:6 [▼
    "points" => 10
    "scoredGoals" => 15
    "goalsConceded" => 20
    "wins" => 20
    "loses" => 20
    "draws" => 20
  ]
  "secondTeam" => array:6 [▼
    "points" => 10
    "scoredGoals" => 15
    "goalsConceded" => 20
    "wins" => 20
    "loses" => 20
    "draws" => 20
  ]
  "ThirdTeam" => array:6 [▼
    "points" => 10
    "scoredGoals" => 15
    "goalsConceded" => 20
    "wins" => 20
    "loses" => 20
    "draws" => 20
  ]
]

如何将数据提取到数组

您可以尝试以下方法。您必须为所有这些需要的额外字段添加:

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')
        where('league_id', '=', $league->id)->get();

    foreach ($matches as $match) {
        $homeTeamScore = $match->score->home_team_score;
        $awayTeamScore = $match->score->away_team_score;

        $standings[$match->homeTeam->name] ??= $blank;
        $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']++;
        }
    }

    return view('admin.leagues.standings')->with('standings',$standings);
}
公共功能排名(联赛$League,团队$Team)
{
$排名=[];
$blank=[
“点数”=>0,
'scoredGoals'=>0,
“失球”=>0,
“wins”=>0,
“丢失”=>0,
“绘制”=>0,
];
$matches=Match::with('score'、'homeTeam'、'awayTeam')
其中('league_id','=',$league->id)->get();
foreach($matches作为$match进行匹配){
$homeTeamScore=$match->score->home\u team\u score;
$awayTeamScore=$match->score->away\u team\u score;
$standings[$match->homeTeam->name]??=$blank;
$standings[$match->awayTeam->name]??=$blank;
$home=&$standings[$match->HOMENTEAM->name];
$away=&$standings[$match->awayTeam->name];
$away['scoredGoals']+=$awayTeamScore;
$home['scoredGoals']+=$homeTeamScore;
$away['GoalsLessed']+=$homeTeamScore;
$home['GoalsLessed']+=$awayTeamScore;
交换机($homeTeamScore$awayTeamScore){
案例1:
//失去家园
//调换主客,让它落空
$tmpHome=&$home;
$home=&$AWEE;
$away=&$tmpHome;
案例1:
//本垒打赢
$home['点数]+=3;
$home['wins']++;
$away['loss']++;
打破
违约:
//画
$home['points']++;
$away['points']++;
$home['draws']++;
$away['draws']++;
}
}
返回视图('admin.leagues.standings')->带有('standings',$standings);
}

您可以试试这样的方法。您必须为所有这些需要的额外字段添加:

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')
        where('league_id', '=', $league->id)->get();

    foreach ($matches as $match) {
        $homeTeamScore = $match->score->home_team_score;
        $awayTeamScore = $match->score->away_team_score;

        $standings[$match->homeTeam->name] ??= $blank;
        $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']++;
        }
    }

    return view('admin.leagues.standings')->with('standings',$standings);
}
公共功能排名(联赛$League,团队$Team)
{
$排名=[];
$blank=[
“点数”=>0,
'scoredGoals'=>0,
“失球”=>0,
“wins”=>0,
“丢失”=>0,
“绘制”=>0,
];
$matches=Match::with('score'、'homeTeam'、'awayTeam')
其中('league_id','=',$league->id)->get();
foreach($matches作为$match进行匹配){
$homeTeamScore=$match->score->home\u team\u score;
$awayTeamScore=$match->score->away\u team\u score;
$standings[$match->homeTeam->name]??=$blank;
$standings[$match->awayTeam->name]??=$blank;
$home=&$standings[$match->HOMENTEAM->name];
$away=&$standings[$match->awayTeam->name];
$away['scoredGoals']+=$awayTeamScore;
$home['scoredGoals']+=$homeTeamScore;
$away['GoalsLessed']+=$homeTeamScore;
$home['GoalsLessed']+=$awayTeamScore;
交换机($homeTeamScore$awayTeamScore){
案例1:
//失去家园
//调换主客,让它落空
$tmpHome=&$home;
$home=&$AWEE;
$away=&$tmpHome;
案例1:
//本垒打赢
$home['点数]+=3;
$home['wins']++;
$away['loss']++;
打破
违约:
//画
$home['points']++;
$away['points']++;
$home['draws']++;
$away['draws']++;
}
}
返回视图('admin.leagues.standings')->带有('standings',$standings);
}

谢谢!您可以帮助在视图文件中显示此数组吗。尝试这样的smth:{{$team_name}{{$points}}但出现错误..这不能正常工作。当团队有多场比赛时,数组不计算结果。谢谢!您可以帮助在视图文件中显示此数组吗。尝试这样的smth:{{$team_name}{{$points}}但出现错误..这不能正常工作。当团队有多场比赛时,数组不计算结果。