Dev.Wol Dev.Wol - 22 days ago 4
PHP Question

Count Number of Items In array above 0

I have an array

array:3 [▼
1 => "2"
2 => "2"
3 => "0"
]


I want to count the number of keys which have the value over 0. In the above example I should have the answer 2.

Here's my attempt.

return count($input_items > 0);


This returns 1

Answer

You can use array_filter as:

count(array_filter($array))

If you don’t supply a callback function, it filters all values out that equal false (boolean conversion).

Comments