Answer the question
In order to leave comments, you need to log in
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;
}
// $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 и т.д.
echo get_sum($values);
// Вариант-1 выводит все верно = 108.74
// Вариант-2 выводит только последнее значение в цикле, т.е. не суммирует.
(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 questionAsk a Question
731 491 924 answers to any question