iterator_apply

iterator_apply

(PHP 5 >= 5.1.0, PHP 7)

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 supplied function to every member of an array

© 1997–2017 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://secure.php.net/manual/en/function.iterator-apply.php

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部