$dates1[] = [
[
'date' => "01.08.2023",
'qty' => 100
],
[
'date' => "02.08.2023",
'qty' => 15,
],
];
$dates1[] = [
[
'date' => "01.08.2023",
'qty' => 0
],
[
'date' => "03.08.2023",
'qty' => 25,
],
];
$result = [
[
'date' => "01.08.2023",
'qty' => 100
],
[
'date' => "02.08.2023",
'qty' => 15,
],
[
'date' => "03.08.2023",
'qty' => 25,
],
];
function merge($arrs, $idKey, ...$sumKeys) {
$result = [];
foreach (array_merge(...$arrs) as $n) {
$id = $n[$idKey];
$result[$id] ??= array_merge($n, array_combine($sumKeys, array_fill(0, count($sumKeys), 0)));
foreach ($sumKeys as $k) {
$result[$id][$k] += $n[$k];
}
}
return array_values($result);
}
$result = merge($dates1, 'date', 'qty');
function merge($arr) {
$map = array_reduce(array_merge(...$arr), function($map, $item) {
$map[$item["date"]] = ($map[$item["date"]] ?? 0) + $item["qty"];
return $map;
}, []);
return array_map(function($key) use ($map) {
return [
"date" => $key,
"qty" => $map[$key],
];
}, array_keys($map));
}