D
D
deespe12018-03-20 09:31:03
PHP
deespe1, 2018-03-20 09:31:03

How to calculate the sum of all numbers?

There is a code that displays numbers. Can you tell me how to display only the sum of these numbers?

<?php foreach (get_payment_system('invest') as $item) { ?>

  <?php echo format_currency(login_profit($login, $item['value']), $val) . ' ' . html($valu); ?>

  <?php } ?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DevMan, 2018-03-20
@deespe1

Well, something like this:

<?php 
$sum = 0;
foreach (get_payment_system('invest') as $item) {
    $sum += $item['value'];
}
echo $sum;
?>

A
Anton, 2018-03-20
@Eridani

Take out and declare the variable $sum=0 for the loop above, and in the loop add to this variable the values ​​$sum += here are your numbers

S
Sanovskiy, 2018-03-20
@Sanovskiy

Do not fence bicycles out of crutches.
Also do not produce unnecessary variables and cycles.
There are a lot of functions for working with arrays.

$vals = [1,2,3,4,5];
$sum = array_reduce($vals,function($carry, $item){return $carry+$item;});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question