How to find the Sum of Odd numbers in arrays by php
This article mainly explains "php how to find the sum of odd numbers in an array", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's make up a small series to take you to learn "php how to find the odd sum in the array"!
Methods: 1. Define a variable with "$sum=0;" to store the calculation result;2. Loop through the array with "foreach($arr as $v){}";2. In the loop body, check whether the array elements are odd, and if so, add them together, syntax "if($v%2!= 0){$sum+=$v;}"。
Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer
Php Find the odd sum in an array
Realization of ideas:
Define a variable assigned 0 to store the result of the calculation.
Loop through arrays using foreach statements
In the loop body, check array elements one by one for odd numbers; add them if they are odd
Check for odd numbers:
Determine whether the element is divisible by 2, and if not, odd. Grammar:
Element %2!= 0
Complete implementation code:
As you can see, in the above example, the odd elements are: 1, 3, 5, 7, 9, we use the counter to add up is not 25
At this point, I believe that everyone has a deeper understanding of "how to find the odd sum of arrays in php", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!