M
M
Msklf2018-01-31 12:21:20
PHP
Msklf, 2018-01-31 12:21:20

Is it correct to put an array in a php loop?

Good afternoon,
I want to sum the values ​​of cells in a loop.
There is a function:

function get_sum($arr) {
    $sum = 0.00;
    foreach($arr as $elem)
        $sum += $elem;
    return $sum;
}

Collecting an array in a loop:
//  $values = array(1.12,2.33,5.05,100.24); // Вариант-1
    $values = array($row['go_rs']); // Пробовал и array($row['go_rs'] . ' '). Вариант-2
// если вывести echo ($row['go_rs'] . ' '); в цикле то получим: 342.12 1244.24 2352.14 и т.д.

And output
echo get_sum($values); 
// Вариант-1 выводит все верно = 108.74
// Вариант-2 выводит только последнее значение в цикле, т.е. не суммирует.

Those. the problem is in the formation of the array, in theory it should be of this form. (n1,n2,n3,n4)If you add , there is a problem with the last comma. Can you tell me how to correctly collect an array to display values? array($row['go_rs'] . ',')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question