kev_m kev_m - 6 months ago 21
PHP Question

add database value off array code igniter php

hi i want to add the values that i got from database..

i have an output of

Array (
[0] => stdClass Object ( [TOTAL_TIME] => 10.58 )
[1] => stdClass Object ( [TOTAL_TIME] => 9.23 )
[2] => stdClass Object ( [TOTAL_TIME] => 10.35 )
[3] => stdClass Object ( [TOTAL_TIME] => 10.10 )
[4] => stdClass Object ( [TOTAL_TIME] => 9.95 )
[5] => stdClass Object ( [TOTAL_TIME] => 3.40 )
[6] => stdClass Object ( [TOTAL_TIME] => 9.90 )
[7] => stdClass Object ( [TOTAL_TIME] => 10.63 )
[8] => stdClass Object ( [TOTAL_TIME] => 10.48 )
[9] => stdClass Object ( [TOTAL_TIME] => 9.43 )
[10] => stdClass Object ( [TOTAL_TIME] => 6.42 )
[11] => stdClass Object ( [TOTAL_TIME] => 10.12 )
[12] => stdClass Object ( [TOTAL_TIME] => 9.33 )
[13] => stdClass Object ( [TOTAL_TIME] => 5.53 )
[14] => stdClass Object ( [TOTAL_TIME] => 9.35 )
[15] => stdClass Object ( [TOTAL_TIME] => 9.60 )
[16] => stdClass Object ( [TOTAL_TIME] => 10.08 )
[17] => stdClass Object ( [TOTAL_TIME] => 10.03 )
[18] => stdClass Object ( [TOTAL_TIME] => 7.73 )
[19] => stdClass Object ( [TOTAL_TIME] => 16.82 )
[20] => stdClass Object ( [TOTAL_TIME] => 16.55 ) )


i want to do is to add those value.

i alredy did

$sum = array_sum($data)


but it does not work. the only output i got was 0.

how can i add it? pls help.

Answer

Loop over the array and sum up the values of the TOTAL_TIME property of each object together.

$sum = 0;
foreach ($array as $object){
    $sum += $object->TOTAL_TIME;
}

print $sum;