-
-
Notifications
You must be signed in to change notification settings - Fork 13
IEnumerable.selectMany() method
Marcel Kloubert edited this page Nov 2, 2021
·
3 revisions
Projects each element of that sequence to a new sequence and flattens the results into one flatten sequence (s. SelectMany()).
public function selectMany(callable $selector) : IEnumerable;
Name | Type | Description |
---|---|---|
$selector | callable | The selector that transforms the current element to a sequence. |
A callable that transforms the current element to a sequence.
function (mixed $item,
IIndexedItemContext $ctx) : sequence;
The current item.
The item context for $item
.
The new sequence.
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
}
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
}