Getting total value of an array with condition

前端 未结 3 633
春和景丽
春和景丽 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

    Here's a quick way:

    $total = array_sum(array_filter($aReeks, function($n) { return $n > 0; }));
    
    • Filter the array for values greater than 0
    • Sum that array

    Oh I now see the "I have to do it with a for loop.", so this won't be accepted for your homework I imagine.

提交回复
热议问题