iterator_count
(PHP 5 >= 5.1.0)
iterator_count — 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);
?>
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)
?>