PHP 7.1.2 Released

iterator_count

(PHP 5 >= 5.1.0, PHP 7)

iterator_countCount the elements in an iterator

Description

int iterator_count ( Traversable $iterator )

Count the elements in an iterator. iterator_count() is not guaranteed to retain the current position of the 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)

Example #2 iterator_count() modifies position

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

The above example will output:

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

Example #3 iterator_count() in foreach loops

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

The above example will output:

0: one (3)

add a note add a note

User Contributed Notes 1 note

up
0
woody dot gilk at gmail dot com
4 days ago
When called, this function is effectively the same as:

<?php
$count
= 0;
$iterator->rewind();
while (
$iterator->valid()) {
   
$count++;
   
$iterator->next();
}
return
$count;
?>

If you ever need to create a mock of an iterator that is passed to iterator_count(), these are the methods to account for.
To Top