【问题标题】:how to add additional value to array如何向数组添加附加值
【发布时间】:2021-05-03 12:10:53
【问题描述】:

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

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

【问题讨论】:

  • 如何知道数组中的哪个元素属于哪个团队?元素键 (First team) 是 teams 表中的团队名称吗?
  • @Unflux,是的,数组元素键“First team”是teams表中的值,其中值“name”
  • 那段代码是不是很眼熟

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


【解决方案1】:

collection 中每个元素的key 一起使用是team 的名称,并存储在teams 表的name 列中,您可以映射您的集合并添加你的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;
}

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

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

【讨论】:

  • 我在下面添加了我的代码,也许你可以帮助编辑它们。我将不胜感激
  • @Jetz 最好将其添加到您的原始问题中,而不是作为答案伙伴。
猜你喜欢
  • 2017-06-05
  • 2015-12-20
  • 1970-01-01
  • 2021-01-10
  • 2013-05-17
  • 2017-03-14
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多