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

search for in the

iterator_count> <class_uses
[edit] Last updated: Fri, 26 Apr 2013

view this page in

iterator_apply

(PHP 5 >= 5.1.0)

iterator_applyCall a function for every element in an iterator

Description

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

Calls a function for every element in an iterator.

Parameters

iterator

The class to iterate over.

function

The callback function to call on every element.

Note: The function must return TRUE in order to continue iterating over the iterator.

args

Arguments to pass to the callback function.

Return Values

Returns the iteration count.

Examples

Example #1 iterator_apply() example

<?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));
?>

The above example will output:

APPLES
BANANAS
CHERRIES

See Also

  • array_walk() - Apply a user function to every member of an array



add a note add a note User Contributed Notes iterator_apply - [1 notes]
up
-2
kminkler at synacor dot com
3 years ago
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;
}

?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites