PHP 7.1.21 Released

iterator_apply

(PHP 5 >= 5.1.0, PHP 7)

iterator_applyCall a function for every element in an iterator

Açıklama

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

Calls a function for every element in an iterator.

Değiştirgeler

iterator

The iterator object to iterate over.

function

The callback function to call on every element. This function only receives the given args, so it is nullary by default. If count($args) === 3, for instance, the callback function is ternary.

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

args

An array of arguments; each element of args is passed to the callback function as separate argument.

Dönen Değerler

Returns the iteration count.

Örnekler

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

Yukarıdaki örneğin çıktısı:

APPLES
BANANAS
CHERRIES

Ayrıca Bakınız

  • array_walk() - Bir dizinin her üyesine kullanıcı tanımlı bir işlevi uygular

add a note add a note

User Contributed Notes 1 note

up
0
ycgambo at outlook dot com
8 months ago
$args is an array and each of its elements are passed to the callback as separate arguments.

so this is the right way to get args:

<?php
$ai
= new ArrayIterator(range(0, 2));

iterator_apply($ai, function() {
   
var_dump(func_get_args());     // use this func
   
return true;
}, array(
1, 2));
?>

output:

array(2) {
  [0] =>
  int(1)
  [1] =>
  int(2)
}
array(2) {
  [0] =>
  int(1)
  [1] =>
  int(2)
}
array(2) {
  [0] =>
  int(1)
  [1] =>
  int(2)
}

--------------------------------------------------
or list each args:

<?php
$ai
= new ArrayIterator(range(0, 2));

iterator_apply($ai, function($arg1, $arg2, $arg3) {
   
var_dump([$arg1, $arg2, $arg3]);
    return
true;
}, array(
1, 2));
?>

same output.
To Top