-
-
Notifications
You must be signed in to change notification settings - Fork 13
IEnumerable.toDictionary() method
Marcel Kloubert edited this page Sep 27, 2015
·
6 revisions
Converts that sequence to a new dictionary (s. ToDictionary()).
public function toDictionary([ equality_comparer $keyComparer = null
[, value_validator $keyValidator = null
[, value_validator $valueValidator = null ]]]) : IDictionary;
Name | Type | Description |
---|---|---|
$keyComparer | [[equality_comparer | Equality comparer]] |
$keyValidator | [[value_validator | Value validator]] |
$valueValidator | [[value_validator | Value validator]] |
Compares if two keys are the same.
Validates if a value is valid to use as key.
Validates if a value is valid to use as value.
The new dictionary.
use \System\Linq\Enuermable;
$seq = Enumerable::create(['A' => 239,
'b' => 5979,
'c' => 1]);
// ['A'] => 239
// ['b'] => 5979
// ['c'] => 1
$dict = $seq->toDictionary();
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 '];
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 '];
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);
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);
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);
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);