Laravel combine un tableau multidimensionnel basé sur la valeur de l'année
P粉193307465
P粉193307465 2024-02-17 21:06:08
0
1
371

C'est le tableau que je souhaite combiner en fonction des valeurs de l'année

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
  ]

Besoin d'une telle sortie

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

répondre à tous(1)
P粉633075725

Essayez ceci :

$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);
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal