0

I have a model object that comes back for a particular user, where I var_dumped object and got an array that looks like this:

array(2) {
  [0]=>
  array(10) {
    ["ticket_labor_ot_travel_c"]=>
    string(5) "34.50"
    ["ticket_labor_travel_c"]=>
    string(5) "23.00"
    ["ticket_labor_ot_c"]=>
    string(5) "34.50"
    ["ticket_labor_reg_c"]=>
    string(5) "23.00"
    ["ticket_labor_user_id"]=>
    string(3) "319"
    ["ticket_labor_tot_hours"]=>
    string(4) "0.50"
    ["ticket_labor_reg_hours"]=>
    string(4) "0.50"
    ["ticket_labor_ot_hours"]=>
    string(4) "0.00"
    ["ticket_labor_travel_hours"]=>
    string(4) "0.00"
    ["ticket_labor_ot_travel_hours"]=>
    string(4) "0.00"
  }
  [1]=>
  array(10) {
    ["ticket_labor_ot_travel_c"]=>
    string(4) "0.00"
    ["ticket_labor_travel_c"]=>
    string(4) "0.00"
    ["ticket_labor_ot_c"]=>
    string(4) "0.00"
    ["ticket_labor_reg_c"]=>
    string(4) "0.00"
    ["ticket_labor_user_id"]=>
    string(1) "0"
    ["ticket_labor_tot_hours"]=>
    string(4) "0.00"
    ["ticket_labor_reg_hours"]=>
    string(4) "0.00"
    ["ticket_labor_ot_hours"]=>
    string(4) "0.00"
    ["ticket_labor_travel_hours"]=>
    string(4) "0.00"
    ["ticket_labor_ot_travel_hours"]=>
    string(4) "0.00"
  }
}

Now the problem I am having is that, One: this model is called in side a for loop, thus I can get back many of these arrays (each of these arrays is tied to a different user) and Two: each array can have, more then two arrays inside of it, in fact some of the arrays that come back can have up to 40.

So what I want to do is essentially, if we use the above array as an example, take that array and create:

array(2) {
  [0]=>
  array(10) {
    ["ticket_labor_ot_travel_c"]=>
    string(5) "34.50"
    ["ticket_labor_travel_c"]=>
    string(5) "23.00"
    ["ticket_labor_ot_c"]=>
    string(5) "34.50"
    ["ticket_labor_reg_c"]=>
    string(5) "23.00"
    ["ticket_labor_user_id"]=>
    string(3) "319"
    ["ticket_labor_tot_hours"]=>
    string(4) "0.50"
    ["ticket_labor_reg_hours"]=>
    string(4) "0.50"
    ["ticket_labor_ot_hours"]=>
    string(4) "0.00"
    ["ticket_labor_travel_hours"]=>
    string(4) "0.00"
    ["ticket_labor_ot_travel_hours"]=>
    string(4) "0.00"
  }
}

the above array is created by taking all the arrays inside the array of arrays returned, adding all their $key=>$value's together and returning one array with all its totals. So in the case of the example above, I took the two arrays inside the array returned, created one array with the same key structure and added all the values together, in this case each was 0, thus the resulting value of each key is 0.

The problem is - I cannot find a clean, efficient way to do this, all the approaches I have tried are massive for loops that try and store each value....

Is there a clean OOP way of doing this?

4

1 回答 1

1

我不认为这是巨大的:

$result = array();
foreach ($container as $innerArray) {
    foreach ($innerArray as $key=>$value) {
        $result[$key] = number_format($result[$key] + $value, 2);
    }
}
var_dump($result);

这是一个演示。我更改了几个值以表明它们实际上是被添加的。

于 2013-11-06T19:24:54.907 回答