0

I am using Codeigniter and I have a function like this .

function total_income(){    
        $data['total_income'] = $this->mod_products->total_income();
        echo"<pre>";
        print_r($data['total_income']);    
}

The above code return an array like this.

Array
(
    [0] => stdClass Object
        (
            [sub_product_price] => 1000
            [quantity] => 1
        )

    [1] => stdClass Object
        (
            [sub_product_price] => 1000
            [quantity] => 1
        )

    [2] => stdClass Object
        (
            [sub_product_price] => 50
            [quantity] => 15
        )

    [3] => stdClass Object
        (
            [sub_product_price] => 500
            [quantity] => 5
        )
)

Now I want to get the [sub_product_price] and multiply that value with [quantity] .Then I want to get the array_sum. I don't have an idea how to do that. Could some one help me It would be grate ,

Cheers!! Rob

0

2 Answers 2

1
$sum = 0;
foreach ($array as $obj) {
    $sum += ($obj->quantity * $obj->sub_product_price);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Add the all values after multiplication in an empty array and use array_sum() to get the final value.

$temp = array();
foreach($data['total_income'] as $in)
{
    $temp[] = $in->sub_product_price * $in->quantity;
}

$total = array_sum($temp);


Explanation:

array_sum() returns the sum of all the values of an array.

Example,

$array = array(4,5,6);
echo array_sum($array);    // 15 

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.