downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

iterator_count> <class_parents
Last updated: Fri, 06 Nov 2009

view this page in

iterator_apply

(PHP 5 >= 5.1.0)

iterator_applyユーザ関数をイテレータのすべての要素でコールする

説明

int iterator_apply ( Traversable $iterator , callback $function [, array $args ] )

イテレータ内のすべての要素に対して関数をコールします。

パラメータ

iterator

順次処理したいクラス。

function

すべての要素に対してコールしたいコールバック関数。

注意: iterator での処理を続けるために、 この関数は TRUE を返さなければなりません。

args

コールバック関数に渡す引数。

返り値

イテレータの要素数を返します。

例1 iterator_apply() の例

<?php
function 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

参考

  • array_walk() - 配列の全ての要素にユーザ関数を適用する



add a note add a note User Contributed Notes
iterator_apply
kminkler at synacor dot com
02-Jun-2009 02:02
To clarify, this method does not work exactly like array_walk(), since the current key/value of the iterator is not passed to the callback function.

This php method is equivalent to:

<?php

function iterator_apply(Traversable $iterator, $function, array $args)
{
   
$count = 0;
    foreach (
$iterator as $ignored)
    {
       
call_user_func_array($function, $args);
       
$count++;
    }

    return
$count;
}

?>

iterator_count> <class_parents
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites