$array = array(1, 2, 3); $count = Arrays::count($array, function ($element) { return $element < 3; });
2
public static function count(array $elements, $predicate) { $count = 0; foreach ($elements as $element) { if (Functions::call($predicate, $element)) { $count++; } } return $count; }
/** * @test */ public function shouldCountElements() { //given $array = array(1, 2, 3); //when $count = Arrays::count($array, function ($element) { return $element < 3; }); //then $this->assertEquals(2, $count); }