Getting total value of an array with condition

前端 未结 3 612
春和景丽
春和景丽 2021-01-29 15:22

I need to get the total value of this array of all the numbers above or equal to 0. This is the array

$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6         


        
3条回答
  •  耶瑟儿~
    2021-01-29 16:05

    You are making 2 major mistakes one is in if condition which is $totaal < $aReeks[$y] you don't need this check at all. Secondly rather than summing up the value of each item to the total of all previous items... you are simply assigning the value to the $totaal variable inside the loop.

    $aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983); 
    $totaal = 0;
    for($y=0; $y < count($aReeks); $y++)
    {
        if($aReeks[$y] > 0)
            $totaal = $totaal + $aReeks[$y];
    }
    

提交回复
热议问题