2

I know this sounds silly but how do I get the total sum of all the object's total?

My json decode format looks something like this:

Array
(
[results] => Array
    (
        [0] => Array
            (
                [total] => 22   
            )
        [1] => Array
            ( 
                [total] => 10
            )
     )
)   

I've tried using writing something like this but it shows "Trying to get property of non-object in..."

echo 'Array Total<pre>';
$sum = 0;
foreach ( $receipt_data['results'] as $receipt )
{
    $sum += $receipt->total;
}
echo '</pre>';
0

1 Answer 1

3

Try this it's works for you You have array so you need to use array instead of object.

echo 'Array Total<pre>';
$sum = 0;
foreach ( $receipt_data['results'] as $receipt )
{
    $sum += $receipt['total'];
}
echo '</pre>';
Sign up to request clarification or add additional context in comments.

2 Comments

sorry remove [0] and check
if you direct use $receipt_data instead of $receipt_data['results'] then you need to use [0]

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.