0
array (
  '' => 
  array (
0 => '0.0000',
1 => '0.0330',
2 => '1100.8486',
3 => '0.3560',
4 => '3.9032',
5 => '0.0000',
6 => '0.8571',
7 => '0.0000',
8 => '0.6660',
9 => '4538.2989',
10 => '0.0000',
11 => '0.1385',
12 => '0.0000',
13 => '0.0000',
14 => '0.0989',
15 => '0.0000',
16 => '0.0000',
17 => '0.6132',
18 => '0.0000',
19 => '12288.1236',
  ),
  'ads_bottom' => 
  array (
    0 => '0.0000',
    1 => '0.0000',
    2 => '5.7823',
    3 => '0.0000',
    4 => '0.0000',
     5 => '0.0000',
    6 => '0.0000',
    7 => '0.0000',
    8 => '0.0000',
    9 => '25.3049',
    10 => '0.0000',
    11 => '0.0000',
    12 => '0.0000',
    13 => '0.0000',
     14 => '0.0000',
    15 => '0.0000',
    16 => '0.0000',
    17 => '101.6482',
  ),
  'ads_side' => 
  array (
    0 => '0.0000',
    1 => '0.0000',
    2 => '1.4440',
    3 => '0.0000',
    4 => '0.0000',
    5 => '0.0000',
    6 => '0.0000',
    7 => '0.0000',
    8 => '0.0000',
    9 => '11.0701',
    10 => '0.0000',
    11 => '0.0000',
    12 => '0.0000',
    13 => '0.0000',
    14 => '0.0000',
    15 => '0.0000',
    16 => '0.0000',
    17 => '29.8609',
      ),
  'ads_top' => 
  array (
    0 => '0.0000',
    1 => '0.0000',
    2 => '50.6098',
    3 => '0.0000',
    4 => '0.0000',
    5 => '0.0000',
    6 => '0.0000',
    7 => '0.0000',
    8 => '0.0000',
    9 => '214.3004',
    10 => '0.0000',
    11 => '0.0000',
    12 => '0.0000',
    13 => '0.0000',
    14 => '0.0000',
    15 => '0.0000',
    16 => '0.0000',
    17 => '548.1496',
      ),
      'image_ad_top' => 
  array (
    0 => '0.0000',
    1 => '0.0000',
    2 => '3.3757',
    3 => '0.0000',
    4 => '0.0000',
    5 => '0.0000',
    6 => '0.0000',
    7 => '0.0000',
    8 => '0.0000',
    9 => '17.3996',
   10 => '0.0000',
   11 => '0.0000',
   12 => '0.0000',
   13 => '0.0000',
   14 => '0.0726',
   15 => '0.0000',
   16 => '0.0000',
   17 => '0.0000',
   18 => '57.1042',
  ),
      'page_next' => 
  array (
    0 => '0.0000',
    1 => '0.0000',
    2 => '60.7305',
    3 => '0.0000',
    4 => '0.0000',
    5 => '0.0000',
    6 => '0.0000',
    7 => '150.2470',
    8 => '0.0000',
    9 => '0.0000',
   10 => '0.0000',
   11 => '0.0000',
   12 => '0.0000',
   13 => '0.0000',
   14 => '0.0330',
   15 => '451.5058',
    ),
 )

How to sum array values of same key and save it to database ? I am stuck in the process of doing foreach for the result I tried to use for each for these but can't seem to get the right output that I wanted

1 Answer 1

1

Use nested foreach statement and create an array with the same keys. For ex,

$temp = array();
foreach($main_array as $key=>$val){
    foreach ($val as $key2=>$val2) {
        if(!isset($temp[$key2]))
            $temp[$key2]=0;
        $temp[$key2]+= $val2; 
    }
}
//$temp will hold a row of same keys sum
Sign up to request clarification or add additional context in comments.

2 Comments

foreach($result as $key => $val){ $acc[$key] += $val; } I did something like this
Did you try using nested(two) foreach statement as in the updated answer?

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.