0

So as of late I have been asking a lot of questions regarding adding the values of all arrays in an array of arrays into one array. That's been fine and dandy - sort of.

I am faced with an issue where I have something, var_dumped that looks like:

array(5) {
  ["AB"]=>
  array(3) {
    ["Unknown"]=>
    array(810) {
      [0]=>
      array(7) {
        ["REG"]=>
        float(0)
        ["COST"]=>
        float(0)
        ["TOT"]=>
        float(0)
        ["OT"]=>
        float(0)
        ["TRVL"]=>
        float(0)
        ["TRV OT"]=>
        float(0)
        ["TOTAL"]=>
        float(0)
      },
      .... (809 More)
    }
    ["SomeName"]=>
    array(810) {
      [0]=>
      array(7) {
        ["REG"]=>
        float(0)
        ["COST"]=>
        float(0)
        ["TOT"]=>
        float(0)
        ["OT"]=>
        float(0)
        ["TRVL"]=>
        float(0)
        ["TRV OT"]=>
        float(0)
        ["TOTAL"]=>
        float(0)
      },
      .... (809 More)
    }
  }
["MB"]=>
array(3) {
  ["SomeName"]=>
  array(110) {
      [0]=>
      array(7) {
        ["REG"]=>
        float(0)
        ["COST"]=>
        float(0)
        ["TOT"]=>
        float(0)
        ["OT"]=>
        float(0)
        ["TRVL"]=>
        float(0)
        ["TRV OT"]=>
        float(0)
        ["TOTAL"]=>
        float(0)
      },
      .... (109 More)
    }
  }
}

Note: I tried to format the array based on the data that came back, this is all real data minus the actual 800 some tickets - theirs also other provinces and more users.

As you can see in each province is a user, that user can appear in more then one province, with varying degrees of "ticket" based information. For example in Alberta (AB), SomeName has 810 tickets where as in MB that same user has 110 tickets.

So I wanted to add all these "arrays" under the user into one array, so instead of Unknown having 810 arrays they have 1 with all the values added up, keeping the same key structure.

So I wrote this:

// $array  contains the sample array above
foreach($array as $province=>$user){
    foreach($user as $userName=>$tickets){
        $result = array();
        foreach ($tickets as $ticket) {
            foreach ($ticket as $key=>$value) {
                $result[$key] = number_format($result[$key] + $value, 2);
                $array[$province][$userName][] = $result;
            }
        }
    }
}

But unless I am doing something wrong - I come back with 910 tickets for Unknown instead of one array.

Ultimately Unknown should look like:

    ["Unknown"]=>
    array(1) {
      [0]=>
      array(7) {
        ["REG"]=>
        float(0)
        ["COST"]=>
        float(0)
        ["TOT"]=>
        float(0)
        ["OT"]=>
        float(0)
        ["TRVL"]=>
        float(0)
        ["TRV OT"]=>
        float(0)
        ["TOTAL"]=>
        float(0)
     }
    }
    ...

Can some one tell me what I am doing wrong?

Community
  • 1
  • 1
LogicLooking
  • 916
  • 1
  • 16
  • 32
  • Bah, I would have helped you if I could understand what you wanted to do. Your examples give me nothing. :p What I do see is that you create an array called $result and keep building it up and keep adding each version of it onto an array you're traversing with foreach. :p – Horse SMith Nov 07 '13 at 16:00
  • My example is pretty clear. I want to take the first array I have given you and convert it to the last array. so instead of `unknown` having 810 arrays in it, it has one, where each array has been "merged" into one array and the values have for each key has been added from each array so for example `TOTAL` might be 7896 @HorseSMith – LogicLooking Nov 07 '13 at 16:07

1 Answers1

0

Could this fix it for you?

$all = array();
foreach($array as $province=>$user){
    foreach($user as $userName=>$tickets){
        $result = array();
        foreach ($tickets as $ticket) {
            foreach ($ticket as $key=>$value) {
                $result[$key] = number_format($result[$key] + $value, 2);

            }
        }
        $all[$province][$userName] = $result;
    }
}
echo '<pre>';
print_r($all);
echo '</pre>';
Horse SMith
  • 1,003
  • 2
  • 12
  • 25