Ouzo\Utilities\Arrays::count PHP Метод

count() публичный статический Метод

Example: $array = array(1, 2, 3); $count = Arrays::count($array, function ($element) { return $element < 3; }); Result: 2
public static count ( array $elements, callable $predicate ) : integer
$elements array
$predicate callable
Результат integer
    public static function count(array $elements, $predicate)
    {
        $count = 0;
        foreach ($elements as $element) {
            if (Functions::call($predicate, $element)) {
                $count++;
            }
        }
        return $count;
    }

Usage Example

Пример #1
0
 /**
  * @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);
 }