What is the potential solution for summing up arrays with duplicate event_mat_id values in PHP?

When summing up arrays with duplicate event_mat_id values in PHP, one potential solution is to use a foreach loop to iterate through the arrays and create a new array where the values are summed up based on the event_mat_id. You can use the event_mat_id as keys in the new array and add up the values for each duplicate event_mat_id.

// Sample arrays with duplicate event_mat_id values
$arrays = [
    ['event_mat_id' => 1, 'value' => 10],
    ['event_mat_id' => 2, 'value' => 20],
    ['event_mat_id' => 1, 'value' => 5],
    ['event_mat_id' => 3, 'value' => 15],
    ['event_mat_id' => 2, 'value' => 30]
];

// Summing up arrays with duplicate event_mat_id values
$summedArray = [];
foreach ($arrays as $array) {
    if (isset($summedArray[$array['event_mat_id']])) {
        $summedArray[$array['event_mat_id']] += $array['value'];
    } else {
        $summedArray[$array['event_mat_id']] = $array['value'];
    }
}

// Output the summed array
print_r($summedArray);