如何向数组添加附加值

时间:2021-01-29 14:54:42

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

我有一个数组,其中所有数据都由匹配表中的记录计算:

Illuminate\Support\Collection {#1342 ▼
  #items: array:4 [▼
    "First team" => & array:6 [▼
      "points" => 3
      "scoredGoals" => 6
      "goalsConceded" => 6
      "wins" => 0
      "loses" => 0
      "draws" => 3
    ]
    "Second team" => array:6 [▶]
    "third team" => array:6 [▶]
    "fourth team" => & array:6 [▶]
  ]
}

我需要添加到每个团队的数组图像(来自团队表,其中列图像) 我该怎么做?

这是我来自控制器的代码,其中所有数据都是从匹配表中计算出来的:

有我需要编辑的代码:

$排名 = [];

$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,
]);

1 个答案:

答案 0 :(得分:1)

key 中每个元素的 collection 一起是 team 的名称,并存储在 name 表的 teams 列中,您可以映射您的收藏并添加您的 image

例如:

$images = [
    'First team' => 'first-team.jpg',
    'Second team' => 'second-team.jpg',
    'Third team' => 'third-team.jpg'
];

$teamsWithImages =
    collect([
        "First team" => [
            "points" => 3,
            "scoredGoals" => 6,
            "goalsConceded" => 6,
            "wins" => 0,
            "loses" => 0,
            "draws" => 3,
        ],
        "Second team" => [
            "points" => 3,
            "scoredGoals" => 6,
            "goalsConceded" => 6,
            "wins" => 0,
            "loses" => 0,
            "draws" => 3,
        ],
        "Third team" => [
            "points" => 3,
            "scoredGoals" => 6,
            "goalsConceded" => 6,
            "wins" => 0,
            "loses" => 0,
            "draws" => 3,
        ]
    ])->map(function ($item, $key) use ($images) {

        // You would uncomment this line to retrieve the image
        // from your teams table
        // You also wouldn't need the use ($images) either
        //$item['image'] = Teams::where('name', $key)->first()->image;

        $item['image'] = $images[$key];
        return $item;
    })->all();

dump($teamsWithImages);

更新

根据您添加的代码,您无需map,只需将图片添加到您的 foreach 中即可:

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

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

或者,您仍然可以在对 map 进行排序后使用 standings,但您也可以将图像与其他所有内容一起添加。

$standingsWithImages = $standings
    ->map(function ($item, $key) {
        $item['image'] = Team::where('name', $key)->first()->image;
        return $item;
    })->all();
相关问题