iterator_count

Count the elements in an iterator

&reftitle.description; `int` `iterator_count` ``Traversable``array```iterator`

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

&reftitle.parameters;

`iterator`

- The iterator being counted.

&reftitle.returnvalues;

The number of elements in `iterator`.

&reftitle.changelog;

&Version; | &Description;
8.2.0 | The type of `iterator` has been widened from `Traversable` to ``Traversable``array``.

&reftitle.examples;

### `iterator_count` example

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

&example.outputs;

    int(4)

### `iterator_count` modifies position

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

&example.outputs;

    string(3) "one"
    int(3)
    NULL

### `iterator_count` in &foreach; loops

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

&example.outputs;

    0: one (3)