0

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 already did

$sum = array_sum($data)

But it does not work. the only output I got was 0.

How can I add it?

3
  • Output is not an array >It is an object.So array_sum will not work Commented May 17, 2016 at 12:11
  • how can i add those values Commented May 17, 2016 at 12:12
  • 1
    Possible duplicate of codeigniter, result() vs. result_array() Commented May 17, 2016 at 14:08

5 Answers 5

2

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;

You can also use array_walk as an alternative to the loop

$sum = 0;
array_walk($array,function($object) use (&$sum){
    $sum += $object->TOTAL_TIME;
});

print $sum;
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_reduce:

$total_time = array_reduce($arr, function($carry, $item) {
    return $carry += $item->TOTAL_TIME;
}, 0);

array_reduce @ php.net

Comments

0

Use this code

function convertObjectToArrayWithSum($data)
{
    if (is_object($data))
    {
        $data = get_object_vars($data);
    }
    if (is_array($data))
    {
        $input = array_map(__FUNCTION__, $data);
        array_walk_recursive($input, function($item, $key) use (&$final) {
            $final[$key] = isset($final[$key]) ? $item + $final[$key] : $item;
        });
        return $final;
    }
    return $data;
}
print_r(convertObjectToArrayWithSum($data));

Comments

0

If you want to store in variable sum of value TOTAL_TIME try this

$total_time = 0;
if (!empty($data)) {
    foreach ($data as $value) {
        $total_time += $value->TOTAL_TIME;
    }
}

If you get that data from database, i suggest to use SUM on the query so we return variable and don't need to perform foreach loop.

Comments

0
$array =  (array) $yourObject;

Then you apply array_sum

Comments

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.