合并每个数组的值在PHP数组的数组 [英] Merging the values of each array in an array of arrays in PHP

查看:148
本文介绍了合并每个数组的值在PHP数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有回来为特定的用户,在那里我的var_dump ED对象的模型对象,并得到一个数组,看起来像这样:

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"
  }
}

现在我遇到的问题是,一:这种模式被称为侧循环,所以我能找回许多阵列(每个阵列被绑定到不同的用户)和二:每个阵列可有,它里面两个以上的阵列,实际上一些回来最多可以有40阵列的

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"
  }
}

以上阵列通过取数组的数组内的所有阵列创建返回,将所有的 $键=>的共同$值并返回一个阵列其所有的总计。因此,在实施例的以上的情况下,我把两个数组内的数组返回,创建一个阵列使用相同的密钥的结构和加入的所有值一起,在此情况下,每个是0,因此所得到的值的每一个键是0

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....

是否有这样做的一个干净的OOP方式?

Is there a clean OOP way of doing this?

推荐答案

我不认为这是巨大的:

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

这里是一个示范。我改变了一些值表明,事实上,被添加他们。

Here is a demonstration. I changed a couple values to show that they are, in fact, being added.

这篇关于合并每个数组的值在PHP数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆