iterator_apply
(PHP 5 >= 5.1.0, PHP 7)
iterator_apply — ユーザー関数をイテレータのすべての要素でコールする
説明
iterator_apply
( Traversable $iterator
, callable $function
[, array $args
= null
] ) : int
イテレータ内のすべての要素に対して関数をコールします。
パラメータ
iterator
順次処理したいイテレータオブジェクト
function
すべての要素に対してコールしたいコールバック関数。 この関数は
args
のみを受け取ります。 デフォルトでは引数を渡されません。 たとえばcount($args) === 3
の場合、 コールバック関数は3変数関数です。注意:
iterator
での処理を続けるために、 この関数はtrue
を返さなければなりません。args
コールバック関数に渡す引数。 引数の array。
args
の個々の要素が コールバックfunction
に別々の引数として渡されます。
返り値
イテレータの要素数を返します。
例
例1 iterator_apply() の例
<?phpfunction print_caps(Iterator $iterator) { echo strtoupper($iterator->current()) . "\n"; return TRUE;}$it = new ArrayIterator(array("Apples", "Bananas", "Cherries"));iterator_apply($it, "print_caps", array($it));?>
上の例の出力は以下となります。
APPLES BANANAS CHERRIES