从数据库中获取额外的数据到数组中

Posted

技术标签:

【中文标题】从数据库中获取额外的数据到数组中【英文标题】:fetch additional data in to array from database 【发布时间】:2021-03-23 02:50:56 【问题描述】:

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

这是我的 LeaguesController:

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:2 [▼
  "secondTeam" => 3
  "firstTeam" => 0
]

我想做这样的事情:

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
  ]
]

如何获取数据到数组

【问题讨论】:

【参考方案1】:

你可以试试这样的。你必须为你想要的所有这些额外的字段添加:

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);

【讨论】:

谢谢!你能帮忙在视图文件中展示这个数组吗?尝试这样的事情:$team_name $points 但出现错误.. 这不能正常工作。当球队有多于一场比赛时,数组不计算结果。

以上是关于从数据库中获取额外的数据到数组中的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Android 上的意图中获取额外数据?

PHP从带有数组[]的输入中获取发布数据

将 listView 数据从一个活动发送到另一个活动

Laravel 5 - 从数据透视表额外属性获取属性

使用校验和将字节数组转换为字符串。在 python 中重新迭代时获取额外的空字节

如何从MFC选项卡控件(TabCtrl)获取额外数据?