You can use array_filter to create an array of values that match the criteria that you want to calculate, and then run the count on the result. The following example returns the number of elements in an array with a value greater than 4:
$items = array (1, 2, 3, 4, 5, 6, 7, 8, 9);
$itemsOfInterest = array_filter ($items, function ($elem) {return ((int) $elem > 4);})
echo (count ($itemsOfInterest));
source
share