2

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)); 
     } 
   } 
7
  • 2
    what code you have tried yet, show there. Commented Nov 24, 2016 at 10:22
  • Please post your attempts. What you have tried so far. Commented Nov 24, 2016 at 10:23
  • array_sum(array_walk_recursive($array)); Commented Nov 24, 2016 at 10:23
  • 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)); } } Commented Nov 24, 2016 at 10:24
  • i tried the above. Is there any easy method without looping Commented Nov 24, 2016 at 10:24

5 Answers 5

1

Use a foreach loop.

$totals = [];
foreach($consumption as $day){
    foreach($day as $machine => $number){
        if(!isset($totals[$machine]){
            $totals[$machine] = array_sum($number);
        }else{
            $totals[$machine] = $totals[$machine] + array_sum($number);
        }
    }
}
echo '<pre>';
print_r($totals);
echo '</pre>';

Live demo: http://sandbox.onlinephpfunctions.com/code/ad1f529f5b5428c4ea0b9dda2ed20a8baa657d5b

Sign up to request clarification or add additional context in comments.

2 Comments

i need the output as Array ( [REFRIGERATOR] => 47 [WASHINGMACHINE] => 29 [OVEN] => 28 [TV] => 28 [FAN] => 26 )
I added array_sum, sorry I got confused with your arrays being in one like :P
0

I know its probably not the best way but you can use a multi dimensional foreach and put it in an array like this:

foreach($consumption as $consumption_value) {
    foreach($consumption_value as $key -> $value) {
        if(isset($consumptions[$key])) {
            $consumptions[$key] += $value;
        }
        else {
            $consumptions[$key] = $value;
        }
    }
}

Comments

0

Use below code:

$consumption = array( 'MONDAY' => array('REFRIGERATOR' => array(3, 9, 7), 'WASHINGMACHINE' => array(2, 4, 2, 8)), 'TUESDAY' => array('REFRIGERATOR' => array(5, 3, 8), 'OVEN' => array(4, 1, 4)), 'WEDNESDAY' => array('TV' => array(6, 9), 'REFRIGERATOR' => array(2, 3, 5, 2)), 'THURSDAY' => array('TV' => array(5, 3, 3, 2), 'FAN' => array(4, 9, 8, 5)), 'FRIDAY' => array('WASHINGMACHINE' => array(8, 5), 'OVEN' => array(3, 9, 7)) );

$total_ref = $total_was = $total_oven = $total_tv = $total_fan = 0;
foreach($consumption as $key=>$products)
{
    foreach($products as $productName => $data)
    {
        if(isset($newarr[$productName]))
        {
            $newarr[$productName] = $newarr[$productName] + array_sum($data);
        }
        else
        {
            $newarr[$productName] = array_sum($data);
        }
    }
}

echo "<pre>";
print_r($newarr);

Output

Array
(
    [REFRIGERATOR] => 47
    [WASHINGMACHINE] => 29
    [OVEN] => 28
    [TV] => 28
    [FAN] => 26
)

Live Demo: Click Here

1 Comment

edited with dynamic code. please check it. @PraseethaVasappan
0

You can simply use foreach like as

$result = [];
foreach($consumption as $v)
{
    foreach($v as $v_k => $v_v)
    {
        if(isset($result[$v_k]))
            $result[$v_k] += array_sum($v_v);
        else
            $result[$v_k] = array_sum($v_v);
    }
}

print_r($result);

Working Demo

Comments

0

check the demo. result is,{"REFRIGERATOR":47,"WASHINGMACHINE":29,"OVEN":28,"TV":28,"FAN":26}

Hope it helps.

 <?php
    $consumption = array( 'MONDAY' => array('REFRIGERATOR' => array(3, 9, 7), 'WASHINGMACHINE' => array(2, 4, 2, 8)), 'TUESDAY' => array('REFRIGERATOR' => array(5, 3, 8), 'OVEN' => array(4, 1, 4)), 'WEDNESDAY' => array('TV' => array(6, 9), 'REFRIGERATOR' => array(2, 3, 5, 2)), 'THURSDAY' => array('TV' => array(5, 3, 3, 2), 'FAN' => array(4, 9, 8, 5)), 'FRIDAY' => array('WASHINGMACHINE' => array(8, 5), 'OVEN' => array(3, 9, 7)) );

    $result = [];
    foreach($consumption as $dayCon)
    {
      array_walk($dayCon, function($value, $key) use(&$result)
      {
         if(!isset($result[$key])) $result[$key] = 0;
         $result[$key] += array_sum($value);
      });
    }
    echo json_encode($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.