简体   繁体   中英

Sum a foreach result php

I'm having trouble to sum a result in php, I have this code:

[<?php foreach($results2 as $i) : ?>
                <?php echo $i['count'] ?>,
            <?php endforeach; ?>]

That echos:

0,0,0,0,7,0,2

I want to sum those values and not to echo each value,i have tried using count($i) but it's not working

if loop:

$count = 0;
foreach($results2 as $i) :
     $count = $count + $i['count'];
endforeach; 

if array sum:

array_sum($results2)

结合使用array_sumarray_column

$count = array_sum(array_column($results2, 'count'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM