Reputation: 9449
I'm needing a way to merge several arrays ( probably around 8 ) and sum any duplicate keys or sub-keys.
For example:
$arr1 = [
"Friday" => ["Breakfast" => 32, "Lunch" => 45],
"Sunday" => ["Lunch" => 12]
];
$arr2 = [
"Sunday" => ["Breakfast" => 7, "Lunch" => 3],
"Monday" => ["Breakfast" => 12]
];
$arr3 = [
"Monday" => ["Breakfast" => 31]
];
And the output should be something like this:
array (
'Friday' =>
array (
'Breakfast' => 32,
'Lunch' => 45,
),
'Sunday' =>
array (
'Lunch' => 15,
'Breakfast' => 7,
),
'Monday' =>
array (
'Breakfast' => 43,
),
);
How could I combine this? I've tried using array_map()
.
But that seemed to fail with multidimensional arrays like this. Also tried using foreach()
, but that got pretty convoluted.
Here's my attempt:
$total = array_map( function( $arr1, $arr2, $arr3 ){
return( $arr1 + $arr2 + $arr3 );
}, $arr1, $arr2, $arr3 );
Upvotes: 1
Views: 1429
Reputation: 47864
Because your data structure is fully associative, array_merge_recursive()
will keep your data integrity intact. Once the arrays are merged, you only need to iterate the days' meals, cast the single values as array type (turning them into single-element arrays if not already arrays), then unconditionally all array_sum()
.
Code: (Demo)
$arr1 = [
"Friday" => ["Breakfast" => 32, "Lunch" => 45],
"Sunday" => ["Lunch" => 12]
];
$arr2 = [
"Sunday" => ["Breakfast" => 7, "Lunch" => 3],
"Monday" => ["Breakfast" => 12]
];
$arr3 = [
"Monday" => ["Breakfast" => 31]
];
$result = [];
foreach (array_merge_recursive($arr1, $arr2, $arr3) as $day => $meals) {
foreach ($meals as $meal => $counts) {
$result[$day][$meal] = array_sum((array)$counts);
}
}
var_export($result);
Output:
array (
'Friday' =>
array (
'Breakfast' => 32,
'Lunch' => 45,
),
'Sunday' =>
array (
'Lunch' => 15,
'Breakfast' => 7,
),
'Monday' =>
array (
'Breakfast' => 43,
),
)
To avoid variable-variables in Oleg's answer, you can stuff each of your input arrays into an array, then merge them (to preserve the duplicated day names).
Code: (Demo)
$result = [];
foreach (array_merge([$arr1], [$arr2], [$arr3]) as $array) {
foreach ($array as $day => $meals) {
foreach ($meals as $meal => $count) {
$result[$day][$meal] = ($result[$day][$meal] ?? 0) + $count;
}
}
}
or more efficiently, prepopulate the result array with your first array.
$result = $arr1;
foreach (array_merge([$arr2], [$arr3]) as $array) {
foreach ($array as $day => $meals) {
foreach ($meals as $meal => $count) {
$result[$day][$meal] = ($result[$day][$meal] ?? 0) + $count;
}
}
}
Upvotes: 1
Reputation: 675
Try this solution. You can add any count of arrays. But keep names as $arr1-$maxArraysCount
$arr1 = array(
"Friday" => array(
"Breakfast" => 32,
"Lunch" => 45
),
"Sunday" => array(
"Lunch" => 12
)
);
$arr2 = array(
"Sunday" => array(
"Breakfast" => 7,
"Lunch" => 3
),
"Monday" => array(
"Breakfast" => 12
)
);
$arr3 = array(
"Monday" => array(
"Breakfast" => 31
)
);
$maxArraysCount = 8;
$return = array();
for($i = 1; $i < $maxArraysCount; $i++){
$arr = 'arr' . $i;
if(isset($$arr) && is_array($$arr)){
foreach ($$arr as $day => $value) {
foreach ($value as $eat => $count) {
if(!isset($return[$day][$eat])) $return[$day][$eat] = 0;
$return[$day][$eat] = $count + $return[$day][$eat];
}
}
}
}
echo "<pre>";print_r($return);
Here is output:
Array
(
[Friday] => Array
(
[Breakfast] => 32
[Lunch] => 45
)
[Sunday] => Array
(
[Lunch] => 15
[Breakfast] => 7
)
[Monday] => Array
(
[Breakfast] => 43
)
)
Upvotes: 2