Skip to content

IEnumerable.select() method

Marcel Kloubert edited this page Sep 25, 2015 · 2 revisions

IEnumerable->select($selector) method

Projects each element of that sequence to a new value (s. Select()).

Syntax

public function select(callable $selector) : IEnumerable;

Parameters

Name Type Description
$selector [[callable Callable]]

$selector

A callable that transforms the current element to a new one.

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

$item

The current item.

$ctx

The item context for $item.

Result

The new sequence.

Examples

Lambda expression

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(239, 59.79, null);

// convert items to strings
foreach ($seq->select('$x => (string)$x') as $item) {
    // [0] '239'
    // [1] '59.79'
    // [2] ''
}

Closure

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(239, 59.79, null);

// convert items to strings
foreach ($seq->select(function($x) {
                          return (string)$x;
                      }) as $item) {
    // [0] '239'
    // [1] '59.79'
    // [2] ''
}
Clone this wiki locally