iterator_count

(PHP 5 >= 5.1.0)

iterator_countCount the elements in an iterator

Description

int iterator_count ( Traversable $iterator )

Count the elements in an iterator.

Parameters

iterator

The iterator being counted.

Return Values

The number of elements in iterator.

Examples

Example #1 iterator_count() example

<?php
$iterator 
= new ArrayIterator(array('recipe'=>'pancakes''egg''milk''flour'));
var_dump(iterator_count($iterator));
?>

The above example will output:

int(4)

Коментарии

Safe using:
<?php
$cnt 
iterator_count(clone $iterator);
?>
2020-10-16 03:31:49
http://php5.kiev.ua/manual/ru/function.iterator-count.html
Be aware that counting over NoRewindIterator will make items unavailable:

<?php

$iterator 
= new ArrayIterator(['recipe'=>'pancakes''egg''milk''flour']);
$iterator = new NoRewindIterator($iterator);

var_dump($iterator->current());
var_dump(iterator_count($iterator));
var_dump($iterator->current());
$iterator->rewind(); // Does not work because it's NoRewindIterator
var_dump($iterator->current());
var_dump(iterator_count($iterator));

?>

Output:

<?php

/*
string(8) "pancakes"
int(4)
NULL
NULL
*/
int(0)

?>
2021-09-01 10:20:38
http://php5.kiev.ua/manual/ru/function.iterator-count.html

    Поддержать сайт на родительском проекте КГБ