Laravel 根据年份值组合多维数组
P粉193307465
P粉193307465 2024-02-17 21:06:08
0
1
357

这是我想要根据年份值组合的数组

0 => array:3 [▼
    "year" => 2022
    "total_policies_financed" => 1
    "total_amount_financed" => 280.0
  ]
  1 => array:3 [▼
    "year" => 2022
    "total_policies_financed" => 2
    "total_amount_financed" => 5190.0
  ]
  2 => array:3 [▼
    "year" => 2021
    "total_policies_financed" => 2
    "total_amount_financed" => 5190.0
  ]

需要这样的输出

0 => array:3 [▼
    "year" => 2022,
    "total_count" => 2,
    "total_policies_financed" => 3
    "total_amount_financed" => 5470
  ]
  1 => array:3 [▼
   "year" => 2021,
    "total_count" => 1,
    "total_policies_financed" => 2
    "total_amount_financed" => 5190.0
  ]

P粉193307465
P粉193307465

全部回复(1)
P粉633075725

试试这个:

$arr = collect($array)->groupBy('year')->map(function ($items, $key){
    return [
        'year' => $key,
        'total_count' => $items->count(),
        'total_policies_financed' => $items->sum('total_policies_financed'),
        'total_amount_financed' => $items->sum('total_amount_financed'),
    ];
})->values();

dd($arr);
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板