My input array
$consumption = [
'MONDAY' => [
'REFRIGERATOR' => [
3,
9,
7,
],
'WASHINGMACHINE' => [
2,
4,
2,
8,
],
],
'TUESDAY' => [
'REFRIGERATOR' => [
5,
3,
8,
],
'OVEN' => [
4,
1,
4,
],
],
'WEDNESDAY' => [
'TV' => [
6,
9,
],
'REFRIGERATOR' => [
2,
3,
5,
2,
],
],
'THURSDAY' => [
'TV' => [
5,
3,
3,
2,
],
'FAN' => [
4,
9,
8,
5,
],
],
'FRIDAY' => [
'WASHINGMACHINE' => [
8,
5,
],
'OVEN' => [
3,
9,
7,
],
],
];
I need output as
Output:
Array ( [REFRIGERATOR] => 47 [WASHINGMACHINE] => 29 [OVEN] => 28 [TV] => 28 [FAN] => 26 )
Code attempt (pasted in from comments) [here is some pointless text so that Stack Overflow allows me to add in the code xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
if(!empty($consumption))
{
foreach($consumption as $key=>$val)
{
foreach($val as $skey=>$sval)
{
$outPutArray[$skey][] = array_sum($sval);
}
}
}
foreach($consumption as $k => $val)
{
$result[$k] = array_sum(array_column($val, $k));
foreach($val as $skey=>$sval)
{
$result[$skey] = array_sum(array_column($val, $k));
}
}
array_sum(array_walk_recursive($array));