2

I need to sum totals for 'in' and 'ou', [a] through [t] The array.

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

The following is how I am calculating totals 'in'+'ou'. However, I seem to be stuck in a rut when it comes to the individual totals of 'in' a,b,c,d,e,f,t and 'ou' a,b,c,d,e,f,t.

//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];
            }
        }
    }
}

The output should look something like

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

1 Answer 1

2

This should get you started:

        $sumIN = 0; 
        $sumOU = 0;
        foreach($arr as $innerArr)
        {
            $sumIN += array_sum($innerArr['in']);
            $sumOU += array_sum($innerArr['ou']);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

I need each total IN(a => total, b => total,...) and ou(a => total, b => total,...)

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.