Skip to content

IEnumerable.toDictionary() method

Marcel Kloubert edited this page Sep 27, 2015 · 6 revisions

IEnumerable->toDictionary([$keyComparer [, $keyValidator [, $valueValidator]]]) method

Converts that sequence to a new dictionary (s. ToDictionary()).

Syntax

public function toDictionary([ equality_comparer $keyComparer = null
                             [, value_validator $keyValidator = null
                             [, value_validator $valueValidator = null ]]]) : IDictionary;

Parameters

Name Type Description
$keyComparer [[equality_comparer Equality comparer]]
$keyValidator [[value_validator Value validator]]
$valueValidator [[value_validator Value validator]]

$keyComparer

Compares if two keys are the same.

$keyValidator

Validates if a value is valid to use as key.

$valueValidator

Validates if a value is valid to use as value.

Result

The new dictionary.

Examples

Simple example

use \System\Linq\Enuermable;

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

// ['A'] => 239
// ['b'] => 5979
// ['c'] => 1
$dict = $seq->toDictionary();

Key comparer (lambda expression)

use \System\Linq\Enumerable;

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

$dict = $seq->toDictionary('($x, $y) => 0 === strcasecmp(trim($x), trim($y))');
                           
// all are (true)
// because all will be mapped to 'a'
$a1 = isset($dict['a']);
$a2 = isset($dict['A']);
$a3 = isset($dict[' a  ']);
$a4 = isset($dict['  A  ']);

// all will return 5979
// because all will be mapped to 'B'
$b1 = $dict['b'];
$b2 = $dict['B'];
$b3 = $dict[' b'];
$b4 = $dict[' B  '];

Key comparer (closure)

use \System\Linq\Enumerable;

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

$dict = $seq->toDictionary(function($x, $y) {
                               return 0 === strcasecmp(trim($x), trim($y));
                           });
                           
// all are (true)
// because all will be mapped to 'a'
$a1 = isset($dict['a']);
$a2 = isset($dict['A']);
$a3 = isset($dict[' a  ']);
$a4 = isset($dict['  A  ']);

// all will return 5979
// because all will be mapped to 'B'
$b1 = $dict['b'];
$b2 = $dict['B'];
$b3 = $dict[' b'];
$b4 = $dict[' B  '];

Key validator (lambda expression)

use \System\ArgumentException;
use \System\Linq\Enumerable;

$keyValidator = '$x => is_string($x)';

try {
    $dict1 = Enumerable::create(['a' => 239, 'B' => 5979])
                       ->toDictionary(null, $keyValidator);
}
catch (ArgumentException $ex) {
}

// (true) because all keys are valid
$is1 = isset($dict1);

try {
    $dict2 = Enumerable::create(['a' => 239, 1 => 5979])
                       ->toDictionary(null, $keyValidator);
}
catch (ArgumentException $ex) {
    // code goes here
}

// (false) because the 2nd key is NO string
$is2 = isset($dict2);

Key validator (lambda expression)

use \System\ArgumentException;
use \System\Linq\Enumerable;

$keyValidator = function ($x) {
                    return is_string($x);
                };

try {
    $dict1 = Enumerable::create(['a' => 239, 'B' => 5979])
                       ->toDictionary(null, $keyValidator);
}
catch (ArgumentException $ex) {
}

// (true) because all keys are valid
$is1 = isset($dict1);

try {
    $dict2 = Enumerable::create(['a' => 239, 1 => 5979])
                       ->toDictionary(null, $keyValidator);
}
catch (ArgumentException $ex) {
    // code goes here
}

// (false) because the 2nd key is NO string
$is2 = isset($dict2);

Value validator (lambda expression)

use \System\ArgumentException;
use \System\Linq\Enumerable;

$valueValidator = '$x => !is_string($x)';

try {
    $dict1 = Enumerable::create(['a' => 23.9,
                                 'B' => 5979,
                                 'C' => null,
                                 'd' => false])
                       ->toDictionary(null, null, $valueValidator);
}
catch (ArgumentException $ex) {
}

// (true) because all values are valid
$is1 = isset($dict1);

try {
    $dict2 = Enumerable::create(['a' => 239, 'B' => '5979'])
                       ->toDictionary(null, null, $valueValidator);
}
catch (ArgumentException $ex) {
    // code goes here
}

// (false) because the 2nd key is a string
$is2 = isset($dict2);

Value validator (Closure)

use \System\ArgumentException;
use \System\Linq\Enumerable;

$valueValidator = function ($x) {
                      return !is_string($x);
                  };

try {
    $dict1 = Enumerable::create(['a' => 23.9,
                                 'B' => 5979,
                                 'C' => null,
                                 'd' => false])
                       ->toDictionary(null, null, $valueValidator);
}
catch (ArgumentException $ex) {
}

// (true) because all values are valid
$is1 = isset($dict1);

try {
    $dict2 = Enumerable::create(['a' => 239, 'B' => '5979'])
                       ->toDictionary(null, null, $valueValidator);
}
catch (ArgumentException $ex) {
    // code goes here
}

// (false) because the 2nd value is a string
$is2 = isset($dict2);
Clone this wiki locally