如何对集合中的每条记录进行计算

问题描述 投票:0回答:1

我有一组赌注,我需要计算 var $profit。 每条记录都有赌注和赔率。

这是我的收藏:

Illuminate\Database\Eloquent\Collection {#1900 ▼
  #items: array:4 [▼
    0 => App\Models\Bet {#1912 ▶}
    1 => App\Models\Bet {#1906 ▶}
    2 => App\Models\Bet {#1857 ▶}
    3 => App\Models\Bet {#1882 ▶}
  ]
}

以及数组的属性:

#attributes: array:19 [▼
        "id" => 4
        "user_id" => 1
        "status_id" => 1
        "content" => "some text"
        "sport" => "Basketball"
        "competition" => "Premier league"
        "start_date" => "2021-12-08"
        "bookmaker_id" => 1
        "team1" => "team one"
        "team2" => "team two"
        "selection_id" => 1
        "tip" => "2,5"
        "odds" => "5"
        "unit_id" => 5
        "created_at" => "2021-12-06 13:32:46"
        "updated_at" => "2021-12-06 13:32:46"
        "created_by" => 1
        "updated_by" => null
        "deleted_by" => null
      ]

如何在每个集合数组中进行计算? 例如计算利润(赔率*unit_id(赌注))

php arrays laravel eloquent collections
1个回答
0
投票

这就是我需要的东西:

$bets = array_map(function($item) {
     $item['won'] = number_format((double)$item['odds'] * (int)$item['unit_id'],3);
            return $item;
        }, $bets); 
© www.soinside.com 2019 - 2024. All rights reserved.