Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : How to count array if value > 10 without looping?

Tags:

arrays

php

Is there anyway to count array if value > 10 without looping(foreach)?

$arr=array(5,7,11,67,4,12,15);
$wanted_output=4
like image 608
Logan Avatar asked May 25 '13 09:05

Logan


2 Answers

Just use array_reduce then you don't need count

echo array_reduce($arr, function ($a, $b) {
    return ($b > 10) ? ++$a : $a;
}); // returns 4
like image 70
Baba Avatar answered Sep 30 '22 18:09

Baba


$minVal = 10;
$counter = count(
    array_filter(
        $myArray,
        function($value) use ($minVal) {
            return $value > $minVal;
        }
    )
);
like image 42
Mark Baker Avatar answered Sep 30 '22 18:09

Mark Baker