phpday 2027 - Call for Papers

MultipleIterator::attachIterator

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

MultipleIterator::attachIterator — Attaches iterator information

说明

public function MultipleIterator::attachIterator(Iterator $iterator, string|int|null $info = null): void

Attaches an iterator, together with its associated information. Attached iterators are iterated over in the order in which they were attached.

If iterator is already attached, its associated information is replaced with info, unless info is identical to the information it already has.

参数

iterator

The new iterator to attach.

info

The associated information for the iterator, which must be an int, a string, or null. With MultipleIterator::MIT_KEYS_ASSOC, it is used as the key of the iterator's values in the arrays returned by MultipleIterator::current() and MultipleIterator::key().

返回值

没有返回值。

错误/异常

Throws an InvalidArgumentException if info is not null and is identical to the information associated with an iterator that is already attached.

参见

+添加备注

用户贡献的备注 1 note

up
7
andresdzphp at php dot net
15 years ago
<?php
$ait_id = new ArrayIterator(array('c1001', 'c1002', 'c1003'));
$ait_name = new ArrayIterator(array('apple', 'orange', 'banana'));
$ait_units = new ArrayIterator(array(756, 996, 2345));

$mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$mit->attachIterator($ait_id, "ID");
$mit->attachIterator($ait_name, "NAME");
$mit->attachIterator($ait_units, "UNITS");

echo $mit->countIterators() . "\n"; //3

if ($mit->containsIterator($ait_id)) { //true
    echo "ait_id iterator attached \n";
}

foreach ($mit as $fruit) {
    echo "<pre>";
    print_r($fruit);
    echo "</pre>";
}
?>

Result:

3
ait_id iterator attached

Array
(
    [ID] => c1001
    [NAME] => apple
    [UNITS] => 756
)
Array
(
    [ID] => c1002
    [NAME] => orange
    [UNITS] => 996
)
Array
(
    [ID] => c1003
    [NAME] => banana
    [UNITS] => 2345
)
To Top