PHP.nl

iterator_count

iterator_count

Count the elements in an iterator

int **iterator_count**  $iterator

Count the elements in an iterator. is not guaranteed to retain the current position of the . iterator_count``iterator

iteratorThe iterator being counted.

The number of elements in . iterator

Voorbeeld: example

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

Voorbeeld: modifies position

<?php
$iterator = new ArrayIterator(['one', 'two', 'three']);
var_dump($iterator->current());
var_dump(iterator_count($iterator));
var_dump($iterator->current());
?>
string(3) "one"
int(3)
NULL

Voorbeeld: in foreach loops

<?php
$iterator = new ArrayIterator(['one', 'two', 'three']);
foreach ($iterator as $key => $value) {
    echo "$key: $value (", iterator_count($iterator), ")\n";
}?>
0: one (3)