Skip to content

IEnumerable.selectMany() method

Marcel Kloubert edited this page Nov 2, 2021 · 3 revisions

IEnumerable->selectMany($selector) method

Projects each element of that sequence to a new sequence and flattens the results into one flatten sequence (s. SelectMany()).

Syntax

public function selectMany(callable $selector) : IEnumerable;

Parameters

Name Type Description
$selector callable The selector that transforms the current element to a sequence.

$selector

A callable that transforms the current element to a sequence.

function (mixed $item,
          IIndexedItemContext $ctx) : sequence;

$item

The current item.

$ctx

The item context for $item.

Result

The new sequence.

Examples

Lambda expression

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(1, 2, 3);

foreach ($seq->selectMany('$x => [$x, $x * 10, $x * 100]') as $item) {
    // [0] 1
    // [1] 10
    // [2] 100
    // [3] 2
    // [4] 20
    // [5] 200
    // [6] 3
    // [7] 30
    // [8] 300
}

Closure

use \System\Linq\Enumerable;

$seq1 = Enumerable::fromValues(1, 2, 3);
$seq2 = $seq1->selectMany(function ($x) {
                              return [$x, $x * 10, $x * 100];
                          });

foreach ($seq2 as $item) {
    // [0] 1
    // [1] 10
    // [2] 100
    // [3] 2
    // [4] 20
    // [5] 200
    // [6] 3
    // [7] 30
    // [8] 300
}
Clone this wiki locally