iterator_count
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
iterator_count — 计算迭代器中元素的个数
参数
-
iterator
-
要计数的迭代器。
返回值
迭代器iterator
中的元素个数。
范例
示例 #1 iterator_count() example
<?php
$iterator = new ArrayIterator(array('recipe'=>'pancakes', 'egg', 'milk', 'flour'));
var_dump(iterator_count($iterator));
?>
以上例程会输出:
int(4)

User Contributed Notes 2 notes
oleksii dot bulba at gmail dot com ¶
9 months ago
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)
?>
备份地址:http://www.lvesu.com/blog/php/function.iterator-count.php