Sum Values in Foreach Loop PHP

Sum values in foreach loop php

$sum = 0;
foreach($group as $key=>$value)
{
$sum+= $value;
}
echo $sum;

Sum of values in a foreach loop php

You could use array_sum() with array_column() to compute the total sum of "pay".
Then you could use array_map() to compute the average of individual sum along the total sum.

$data = [
["pay" => "2010000"],
["pay" => "3010000"],
["pay" => "3920000"],
];
$sum = array_sum(array_column($data, 'pay'));
$out = array_map(function($item) use($sum) {
return ['finalResult' => $item['pay'] / $sum] ;
}, $data);
print_r($out);

Outputs:

Array
(
[0] => Array
(
[finalResult] => 0.2248322147651
)

[1] => Array
(
[finalResult] => 0.33668903803132
)

[2] => Array
(
[finalResult] => 0.43847874720358
)

)

Or :

$data = [
["pay" => "2010000"],
["pay" => "3010000"],
["pay" => "3920000"],
];
$sum = array_sum(array_column($data, 'pay'));
$out = array_map(function($item) use($sum) {
return $item['pay'] / $sum ;
}, $data);
print_r($out);

Output:

Array
(
[0] => 0.2248322147651
[1] => 0.33668903803132
[2] => 0.43847874720358
)

sum amounts obtained in foreach

You want the total to accumulate over all the loops, so you define it outside the loop and add to it each time around:

$total = 0;
foreach ($products as $product) {
$subtotal = $product['price']*$product['quantity'];
$total += $subtotal;
}

echo $total;

Sum values received from foreach loop php

You can use a static variable to maintain the sum between function calls:

function custom_function($value) {
static $var = 0;
$var += $value;
echo "$var\n";
}

custom_function(100);
custom_function(200);

Output:

100
300

Demo on 3v4l.org

Sum values of same name in foreach loop php

You can create a new array to group all your results into. Note you may receive notices about offsets, just check firstly the key isset() to stop this or use error_reporting(E_ERROR); to not show warnings.

$groupedResults = array();

foreach ($ga->getResults() as $result) {
$pageviews = $result->getPageviews();
$pagepath = get_part_url($result->getPagePath());
if(is_numeric($pagepath)) $groupedResults[$pagepath] += $pageviews;
}

You can then loop through these and run your DB queries.

foreach ($groupedResults as $k => $v) {
print 'INSERT INTO stats (views,content_id) VALUES ("'.$v.'","'.$k.'")';
}

See a working example here with example data.

sum all value in foreach loop php

Try below code:

<?php 
$total = 0;
foreach((array)$query as $row):
$date = $row->date;
$cost = $row->cost;
if(date("M-d-Y", strtotime($date))==date("M-d-Y")) {
$total = $total+$cost;
}
?>
<td><?php echo $date ?></td>
<td><?php echo $cost ?></td>
<?php endforeach; ?>

<td colspan="2"><?php echo $total; ?></td>

PHP - For each loop - skip if identical values (but still sum values)

Assuming the $input array has at least two columns per row, namely 'CODE' and 'VALUE', we can use the 'CODE' as a key (which is intrinsically unique) to sum the values in our $output array:

$output = array();
foreach($input as $row)
{
$key = $row['CODE'];
$value = $row['VALUE'];
$output[$key] = isset($output[$key]) ? $output[$key] + $value : $value;
}
print_r($output);

Please note that the above solution causes the $output array to grow dynamically, which can be a (time) problem for large arrays. If this is the case, please consider pre-calculating unique values for $input[$i]['CODE'] $i=1 ... count($input) so that you can pre-allocate the $output array by using array_pad.

Sum values in foreach loop in [php]

You could add sum variable for both tot_Price and unit_Price and add in foreach loop

<?php $sum_tot_Price = 0 ?>
<?php $sum_unit_Price = 0 ?>
@foreach ($yourData as X_land)
<tr>
<td>{{ $X_land->house_Type }}</td>
<td class="address">{{ $X_land->the_Location }}</td>
<td>{{ $X_land->tot_Price }}</td>
<td>{{ $X_land->tran_Area }}</td>
<td>{{ $X_land->unit_Price }}</td>
<td><button id="opener<?php echo $opencount?>">詳細資訊</button></td>
</tr>
<?php $sum_tot_Price += $X_land->tot_Price ?>
<?php $sum_unit_Price += $X_land->unit_Price ?>
@endforeach

<tr>
<td>Sum tot_Price {{ $sum_tot_Price}}</td>
<td>Sum unit_Price {{ $sum_unit_Price}}</td>
</tr>


Related Topics



Leave a reply



Submit