MultipleIterator::current

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

MultipleIterator::current — Gets the current elements of the attached iterators

Description

public function MultipleIterator::current(): array

Gets the current elements of all attached iterators, by calling Iterator::current() on each valid one.

With MultipleIterator::MIT_KEYS_NUMERIC, the elements are listed in the order in which the iterators were attached. With MultipleIterator::MIT_KEYS_ASSOC, they are keyed by the information associated with each iterator by MultipleIterator::attachIterator().

Parameters

This function has no parameters.

Return Values

An array containing the current elements of each attached iterator. With MultipleIterator::MIT_NEED_ANY, null is used for the iterators that are not valid.

Errors/Exceptions

Throws a RuntimeException if no iterator is attached, or if MultipleIterator::MIT_NEED_ALL is set and at least one attached iterator is not valid.

Throws an InvalidArgumentException if MultipleIterator::MIT_KEYS_ASSOC is set and an iterator was attached without associated information.

Changelog

Version Description
8.1.0 A RuntimeException is now thrown if MultipleIterator::current() is called when no iterator is attached. Previously, false was returned.

Examples

Example #1 MultipleIterator::current() example

<?php
$iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY | MultipleIterator::MIT_KEYS_ASSOC);
$iterator->attachIterator(new ArrayIterator(['a' => 'apple', 'b' => 'banana']), 'fruit');
$iterator->attachIterator(new ArrayIterator(['x' => 'red']), 'color');

$iterator->rewind();
while ($iterator->valid()) {
    var_dump($iterator->current());
    $iterator->next();
}
?>

The above example will output:

array(2) {
  ["fruit"]=>
  string(5) "apple"
  ["color"]=>
  string(3) "red"
}
array(2) {
  ["fruit"]=>
  string(6) "banana"
  ["color"]=>
  NULL
}

See Also