2013-03-08 4 views
2

Je dois totaliser les totaux pour 'in' et 'ou', [a] à [t] Le tableau.PHP: additionner différentes valeurs d'un tableau multidimensionnel

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

Voici comment je calcule les totaux 'in' + 'ou'. Cependant, je semble être coincé dans une ornière quand il s'agit des totaux individuels de 'dans' a, b, c, d, e, f, t et '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]; 
      } 
     } 
    } 
} 

La sortie devrait ressembler à

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

Répondre

2

Cela devrait vous aider à démarrer:

 $sumIN = 0; 
     $sumOU = 0; 
     foreach($arr as $innerArr) 
     { 
      $sumIN += array_sum($innerArr['in']); 
      $sumOU += array_sum($innerArr['ou']); 
     } 
+0

J'ai besoin chaque total IN (a => total, b => total, .. .) et ou (a => total, b => total, ...) –

Questions connexes