首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

iterator_count

(PHP 5 >= 5.1.0, PHP 7)

iterator_count — Count the elements in an iterator

Description

代码语言:javascript
复制
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

代码语言:javascript
复制
<?php
$iterator?=?new?ArrayIterator(array('recipe'=>'pancakes',?'egg',?'milk',?'flour'));
var_dump(iterator_count($iterator));
?>

The above example will output:

代码语言:javascript
复制
int(4)

Example #2 iterator_count() modifies position

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
string(3) "one"
int(3)
NULL

Example #3 iterator_count() in foreach loops

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

The above example will output:

代码语言:javascript
复制
0: one (3)

← iterator_apply

iterator_to_array →

代码语言:txt
复制
 ? 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com