Php/docs/class.limititerator

提供:Dev Guides
< Php
移動先:案内検索

(PHP 5 >= 5.1.0, PHP 7)

はじめに

LimitIterator クラスは、ある Iterator 上の限られたサブセットに対する反復処理を行います。


クラス概要


LimitIterator extends IteratorIterator implements OuterIterator {

/* メソッド */

public __construct ( Iterator $iterator [, int $offset = 0 [, int $count = -1 ]] )

public current ( ) : mixed

public getInnerIterator ( ) : Iterator

public getPosition ( ) : int

public key ( ) : mixed

public next ( ) : void

public rewind ( ) : void

public seek ( int $position ) : int

public valid ( ) : bool

}

例1 LimitIterator の使用例

<?php// 限定した処理を行うイテレータを作成します$fruits = new ArrayIterator(array(    'apple',    'banana',    'cherry',    'damson',    'elderberry'));// 最初の三つの果物だけをループ対象としますforeach (new LimitIterator($fruits, 0, 3) as $fruit) {    var_dump($fruit);}echo "\n";// 三番目の果物から最後までをループ対象とします// 注意: オフセットは 0 から始まり、0 は apple ですforeach (new LimitIterator($fruits, 2) as $fruit) {    var_dump($fruit);}?>

上の例の出力は以下となります。


string(5) "apple"
string(6) "banana"
string(6) "cherry"

string(6) "cherry"
string(6) "damson"
string(10) "elderberry"

目次