0
have an Array:
$arr = array (1,2,3,4,5,6,7,8,9,10);
I would like to know how to add only even numbers within the array of the vector in PHP?
0
have an Array:
$arr = array (1,2,3,4,5,6,7,8,9,10);
I would like to know how to add only even numbers within the array of the vector in PHP?
2
Use the function array_reduce
which reduces an array to a single value through an iterative process via callback function.
<?php
$arr = array (1,2,3,4,5,6,7,8,9,10);
//Para cada elemento array aplica uma função redutora
$soma = array_reduce($arr, function($carry, $item){
//Testa par ver se o número é par se for soma o elemento senão mantém o valor
return $item % 2 == 0 ? $carry + $item : $carry;
});
echo $soma;
Test in Repl.it
Another possibility is to iterate over the array elements and test to see if they are even
<?php
$arr = array (1,2,3,4,5,6,7,8,9,10);
$soma = 0;
//Para cada elemento do array
foreach($arr as $item){
if ($item % 2 == 0) $soma += $item; //O Soma apenas se for par
}
echo $soma;
Test in Repl.it
Dear Augusto, it is worth mentioning in the answer why used the third parameter as $arr, callbac, 1
, to summarize, the account is multiplication, if the argument were omitted the result would be zero, since $carry
would start as NULL even, then internally PHP would treat the mathematical operation as zero to multiplication https://repl.it/@inphinit/multiplication-vs-null-php
one of the reasons why I did not formulate the answer and I rarely suggest functions such as array_reduce
, is because eventually this leads to one skipping important steps of basic learning, I’m not saying you should follow what I believe, but in almost every area of IT I see the same problem, use of things without real understanding, and I’m forced to give my arm to twist, Soen and pt became copy and glue websites
@Guilhermenascimento I do not know where I got the multiplication from. I made the correction. Yes in multiplication one $carry == 0
would reset the result. I also added an example using loop iteration.
It is not OCD syndrome no (although many people insist that I suffer from this :P) ... but parentheses are not necessary before the ?
in return ($item % 2 == 0) ? $carry + $item : $carry;
... I mean, if it was separating logic or even organizing identation if there were line breaks it would be OK, but there’s a little redundant. PS: +1 by Edit
@Guilhermenascimento that this. All guidance for improvement is welcome. I’m not going to move now because I’m having dinner and I just came by to check it out. When I get back I get it right. But if you feel the need to edit any of my publication I have full confidence in your intervention.
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
Dear Rafael the basic is fundamental, they can even suggest you array_filter, as if it were the solution of all problems, but the basic is essential to learn programming, so it is and foreach will always solve more clearly and reaprovetavel to learn without advanced resources, with the answer https://answall.com/a/110927/3635. it is sufficient inside the IF to check if it is even, add to a variable outside the
for
and after the go cycle will have the result.– Guilherme Nascimento
to sum up would use this: <?php $arrays = array (1,2,3,4,5,6,7,8,9,10); $soma = 0; foreach($arrays as $array){ if(is_int($array/2)){ $soma = $soma+$array; } } echo "sum: ". $sum;
– Marcos Vinicius