close
Skip to content

IEnumerable.toArray() method

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

IEnumerable->toArray([$keySelector]) method

Converts that sequence to a new PHP array (s. ToArray()).

Syntax

public function toArray([ callable $keySelector = null ]) : array;
Name Type Description
$keySelector [[callable Callable]]

$keySelector

The callable that produces the key for an item.

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

$key

The original key of the current item.

$item

The current item.

$ctx

The item context for $item.

Result

The new array.

Examples

Default

use \System\Linq\Enumerable;

$seq = Enumerable::create(['a' => 239, 'b' => 5979, 'c' => 1]);

// [0] => 239
// [1] => 5979
// [2] => 1
$arr = $seq->toArray();

// (true)
$ia = is_array($arr);

Custom key selector (lambda)

use \System\Linq\Enumerable;

$seq = Enumerable::create(['a' => 239, 'b' => 5979, 'c' => 1]);

// ['A'] => 239
// ['B'] => 5979
// ['C'] => 1
$arr = $seq->toArray('$key => strtoupper($key)');

Custom key selector (lambda)

use \System\Linq\Enumerable;

$seq = Enumerable::create(['A' => 239, 'B' => 5979, 'C' => 1]);

// ['a'] => 239
// ['b'] => 5979
// ['c'] => 1
$arr = $seq->toArray(function ($key) {
                         return strtolower($key);
                     });

Clone this wiki locally