array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'de', ), 'this' => array ( 0 => 'function.iterator-count.php', 1 => 'iterator_count', 2 => 'Count the elements in an iterator', ), 'up' => array ( 0 => 'ref.spl.php', 1 => 'SPL Funktionen', ), 'prev' => array ( 0 => 'function.iterator-apply.php', 1 => 'iterator_apply', ), 'next' => array ( 0 => 'function.iterator-to-array.php', 1 => 'iterator_to_array', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/spl/functions/iterator-count.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

iterator_count

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

iterator_countCount the elements in an iterator

Beschreibung

iterator_count(Traversable|array $iterator): int

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

Parameter-Liste

iterator

The iterator being counted.

Rückgabewerte

The number of elements in iterator.

Changelog

Version Beschreibung
8.2.0 The type of iterator has been widened from Traversable to Traversable|array.

Beispiele

Beispiel #1 iterator_count() example

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

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

int(4)

Beispiel #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());
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Beispiel #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";
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

0: one (3)