PHP.mk документација

iterator_apply

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.iterator-apply.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.iterator-apply.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
iterator_apply

Референца за `function.iterator-apply.php` со подобрена типографија и навигација.

function.iterator-apply.php

iterator_apply

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

iterator_apply(PHP 5 >= 5.1.0, PHP 7, PHP 8)

= NULL

iterator_apply(Траверзабилно $iterator, callable $callback, ?array $args = null): int

Повикај функција за секој елемент во итератор

Параметри

iterator

Повикува функција за секој елемент во итератор.

callback

Итератор објектот за итерирање. argsПовратна функција што ќе се повика на секој елемент. Оваа функција прима само дадениот count($args) === 3, така што е нуларна по дифолт. Ако

Забелешка: , на пример, повратната функција е тернарна. true Функцијата мора да врати iterator.

args

Еден array за да продолжи итерацијата над args на аргументи; секој елемент од callback се предава на повратната функција

Вратени вредности

како посебен аргумент.

Примери

Пример #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));
?>

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

APPLES
BANANAS
CHERRIES

Види Исто така

  • array_walk() - Применете корисничка функција на секој член од низа

Белешки од корисници 2 забелешки

Враќа број на итерации.
пред 7 години
Each of the arguments required by the function, must be in the array supplied in the third argument to iterator_apply. You can use references too. Example:

<?php

function translate_to(string $target_language, Iterator $words, array $dictionaries) {
    
    $language = ucfirst($target_language);
    $dictionary = $dictionaries[$target_language] ?? 'not found';
    
    if ($dictionary === 'not found') {
        echo "Not found dictionary for {$language}\n";
        return;
    }
    
    echo "English words translated to {$language}\n";
    
    $not_found = [];
    
    iterator_apply($words, function($words, $dictionary, &$not_found){
    
        $english_word = $words->current();
    
        $translated_word = $dictionary[$english_word] ?? '';
    
        if ($translated_word !== '') {
            echo "{$english_word} translates to {$translated_word}\n";
        } else {
            $not_found[] = $english_word;
        }

        return true;
    
    }, array($words, $dictionary, &$not_found));
    
    echo "\nNot found words:\n" . implode("\n", $not_found) . "\n";
}

$dictionaries = [
    'nahuatl' => [
        'one' => 'Ze',
        'two' => 'Ome',
        'three' => 'Yei',
        'four' => 'Nahui',
    ],
];

$iterator = new \ArrayIterator(array('one', 'two', 'three', 'four', 'gasoil'));

translate_to('nahuatl', $iterator, $dictionaries);
?>

English words translated to Nahuatl
one translates to Ze
two translates to Ome
three translates to Yei
four translates to Nahui

Not found words:
gasoil
Враќа број на итерации.
пред 7 години
Be aware of the proper methods to iterate the specific Iterator you are consuming, as the implementation of the method could vary its behaviour.

For example, unlike the ArrayIterator, you can't iterate on a SplDoubleLinkedList with current() without using next() on every iteration (and then, only would iterate if you return true at the end of the callable. It is far easier then with LinkedLists use a while($it->valid()) { $it->current(); $it->next(); }

Let's see:

<?php

$ll = new \SplDoublyLinkedList();

$ll->push('ze');
$ll->push('ome');
$ll->push('yei');
$ll->push('nahui');

$ll->rewind();

$iterations_done = iterator_apply($ll, function(Iterator $it) {

    echo implode("\t=>", [
        $it->key(),
        $it->current(),
        ucfirst($it->current())
    ]),"\n";
    
    return true;

}, array($ll));

echo "Did iterate {$iterations_done} times \n";

$ll->rewind();

$iterations_done = iterator_apply($ll, function(Iterator $it) {

    echo implode("\t=>", [
        $it->key(),
        $it->current(),
        ucfirst($it->current())
    ]),"\n";
    
    $it->next();
    
    return true;

}, array($ll));

echo "Did iterate {$iterations_done} times \n";

$ll->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE);

var_dump($ll->count());

foreach($ll as $key => $val) {
    echo "{$key}\t",ucfirst($val),"\n";
}

var_dump($ll->count());
?>

Output:

0    =>ze    =>Ze
0    =>ze    =>Ze
0    =>ze    =>Ze
0    =>ze    =>Ze
Did iterate 4 times 
0    =>ze    =>Ze
1    =>ome    =>Ome
2    =>yei    =>Yei
3    =>nahui    =>Nahui
Did iterate 4 times 
int(4)
0    Ze
0    Ome
0    Yei
0    Nahui
int(0)
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.