Reputation: 37
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
Upvotes: 0
Views: 821
Reputation: 8970
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
Upvotes: 0
Reputation: 5090
$sum = 0;
foreach ($array as $obj) {
$sum += ($obj->quantity * $obj->sub_product_price);
}
Upvotes: 1