php - How to get the array sum of each column of each sub array? -


this array:

array ( [hri] => array ( [0] => array ( [0] => 14157.72 [1] => 10157.72 ) [1] => array ( [0] => 39140.94 [1] => 39140.94 ) [2] => array ( [0] => 36383.66 [1] => 36383.66 ) [3] => array ( [0] => 38508.00 [1] => 38508.00 ) [4] => array ( [0] => 8424.00 [1] => 8424.00 ) ) [mpu] => array ( [0] => array ( [0] => 432.00 [1] => 1200.00 ) ) [rha] => array ( [0] => array ( [0] => 111562.50 [1] => 94237.50 ) [1] => array ( [0] => 37880.00 [1] => 31400.00 ) [2] => array ( [0] => 11364.00 [1] => 9420.00 ) [3] => array ( [0] => 23719.50 [1] => 18832.50 ) [4] => array ( [0] => 26705.00 [1] => 21800.00 ) [5] => array ( [0] => 5190.00 [1] => 3816.00 ) [6] => array ( [0] => 8707.50 [1] => 6966.00 ) [7] => array ( [0] => 0.00 [1] => 0.00 ) [8] => array ( [0] => 0.00 [1] => 0.00 ) [9] => array ( [0] => 0.00 [1] => 0.00 ) [10] => array ( [0] => 128952.00 [1] => 94500.00 ) [11] => array ( [0] => 0.00 [1] => 0.00 ) ) [pba] => array ( [0] => array ( [0] => 58816.26 [1] => 50784.36 ) ) ) 

i want go through each subarray , sum each column.

e.g.

for sub array key hri:

[hri] =>      array (         [0] => array ( [0] => 14157.72 [1] => 10157.72 )         [1] => array ( [0] => 39140.94 [1] => 39140.94 )         [2] => array ( [0] => 36383.66 [1] => 36383.66 )         [3] => array ( [0] => 38508.00 [1] => 38508.00 )         [4] => array ( [0] => 8424.00 [1] => 8424.00 )     ) 

expected output:

 column 0:   14157.72 + 39140.94 + 36383.66 + 38508.00 + 8424.00 = ?  column 1:   10157.72 + 39140.94 + 36383.66 + 38508.00 + 8424.00 = ? 

my current code:

$amount = []; for($o=0;$o<count($totalamount);$o++){     print_r($totalamount[$o]['login']);       if($totalamount[$o]['login'] == $totalamount[$o]['login']){         //$amount[$totalamount[$o]['login']] += $totalamount[$o]['piamount'];         $piamount[$totalamount[$o]['login']][] = array_sum(array($totalamount[$o]['piamount'].','));     } } 

how can change code expected result?

this should work you:

just loop through array , sum of each sub array array_sum()

foreach($arr $k => $v)     echo $k . " = " . array_sum($v) . "<br>"; 

example input/output:

$arr = [         [1,2,3],         [4,5,6],         [7,8,9],     ]; 

output:

0 = 6 1 = 15 2 = 24 

edit:

since updated array structure, use this:

foreach($arr $k => $v)     echo $k . " = " . array_sum(array_map("array_sum", $v)) . "<br>"; 

edit 2:

if want sum of each array of sub arrays have use 2 foreach loops this:

foreach($arr $k1 => $innerarray) {     foreach($innerarray $k2 => $v)         echo "$k1-$k2 = " . array_sum($v) . "<br>";     echo "<br><br>"; } 

edit 3:

i think see want: want go through each sub arrays , sum of each column of sub array:

foreach($arr $k1 => $innerarray) {     foreach($innerarray[0] $k2 => $v)         echo "$k1-$k2 = " . array_sum(array_column($innerarray, $k2)) . "<br>";     echo "<br><br>"; } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -