2

我需要对 'in' 和 'ou'、[a] 到 [t] 的数组求和。

Array
(
[1] => Array
    (
        [in] => Array
            (
                [a] => 3
                [b] => 0
                [c] => 0
                [d] => 0
                [e] => 0
                [f] => 0
                [o] => 0
                [t] => 3
            )

        [ou] => Array
            (
                [a] => 0
                [b] => 0
                [c] => 1
                [d] => 0
                [e] => 0
                [f] => 0
                [o] => 0
                [t] => 1
            )
    )
[2] => Array
    (
        [in] => Array
            (
                [a] => 0
                [b] => 0
                [c] => 0
                [d] => 0
                [e] => 0
                [f] => 0
                [o] => 0
                [t] => 0
            )

        [ou] => Array
            (
                [a] => 0
                [b] => 0
                [c] => 0
                [d] => 1
                [e] => 2
                [f] => 0
                [o] => 0
                [t] => 3
            )
    )
)

以下是我如何计算总计'in'+'ou'。但是,当谈到 'in' a,b,c,d,e,f,t 和 'ou' a,b,c,d,e,f,吨。

//get day total
foreach($arr as $array){
    foreach($array as $inou){
        foreach(array_keys($inou) as $value){
            if(isset($total[$value])){
                $total[$value] += $inou[$value];
            }else{
                $total[$value] = $inou[$value];
            }
        }
    }
}

输出应该看起来像

in(
[a] => 3
[b] => 0
[c] => 0
...
[t] => 3
)
ou(
[a] => 0
[b] => 0
[c] => 1
[d] => 1
[e] => 2
[f] => 0
[t] => 4
)
4

1 回答 1

2

这应该让你开始:

        $sumIN = 0; 
        $sumOU = 0;
        foreach($arr as $innerArr)
        {
            $sumIN += array_sum($innerArr['in']);
            $sumOU += array_sum($innerArr['ou']);
        }
于 2013-03-08T23:02:16.160 回答