I'm trying to get counts of array values greater than n
.
I'm using array_reduce()
like so:
$arr = range(1,10);
echo array_reduce($arr, function ($a, $b) { return ($b > 5) ? ++$a : $a; });
This prints the number of elements in the array greater than the hard-coded 5
just fine.
But how can I make 5
a variable like $n
?
I've tried introducing a third argument like this:
array_reduce($arr, function ($a, $b, $n) { return ($b > $n) ? ++$a : $a; });
// ^ ^
And even
array_reduce($arr, function ($a, $b, $n) { return ($b > $n) ? ++$a : $a; }, $n);
// ^ ^ ^
None of these work. Can you tell me how I can include a variable here?