mirror of
https://github.com/yiisoft/yii2.git
synced 2025-10-31 02:28:35 +08:00
Fix #20268: Minor optimisation in \yii\helpers\BaseArrayHelper::map
This commit is contained in:
committed by
GitHub
parent
25059c4e08
commit
e4d5d73490
@ -734,6 +734,57 @@ class ArrayHelperTest extends TestCase
|
||||
'345' => 'ccc',
|
||||
],
|
||||
], $result);
|
||||
|
||||
$result = ArrayHelper::map($array,
|
||||
static function (array $group) {
|
||||
return $group['id'] . $group['name'];
|
||||
},
|
||||
static function (array $group) {
|
||||
return $group['name'] . $group['class'];
|
||||
}
|
||||
);
|
||||
|
||||
$this->assertEquals([
|
||||
'123aaa' => 'aaax',
|
||||
'124bbb' => 'bbbx',
|
||||
'345ccc' => 'cccy',
|
||||
], $result);
|
||||
|
||||
$result = ArrayHelper::map($array,
|
||||
static function (array $group) {
|
||||
return $group['id'] . $group['name'];
|
||||
},
|
||||
static function (array $group) {
|
||||
return $group['name'] . $group['class'];
|
||||
},
|
||||
static function (array $group) {
|
||||
return $group['class'] . '-' . $group['class'];
|
||||
}
|
||||
);
|
||||
|
||||
$this->assertEquals([
|
||||
'x-x' => [
|
||||
'123aaa' => 'aaax',
|
||||
'124bbb' => 'bbbx',
|
||||
],
|
||||
'y-y' => [
|
||||
'345ccc' => 'cccy',
|
||||
],
|
||||
], $result);
|
||||
|
||||
$array = [
|
||||
['id' => '123', 'name' => 'aaa', 'class' => 'x', 'map' => ['a' => '11', 'b' => '22']],
|
||||
['id' => '124', 'name' => 'bbb', 'class' => 'x', 'map' => ['a' => '33', 'b' => '44']],
|
||||
['id' => '345', 'name' => 'ccc', 'class' => 'y', 'map' => ['a' => '55', 'b' => '66']],
|
||||
];
|
||||
|
||||
$result = ArrayHelper::map($array, 'map.a', 'map.b');
|
||||
|
||||
$this->assertEquals([
|
||||
'11' => '22',
|
||||
'33' => '44',
|
||||
'55' => '66'
|
||||
], $result);
|
||||
}
|
||||
|
||||
public function testKeyExists()
|
||||
@ -759,7 +810,7 @@ class ArrayHelperTest extends TestCase
|
||||
if (version_compare(PHP_VERSION, '8.1.0', '>=')) {
|
||||
$this->markTestSkipped('Using floats as array key is deprecated.');
|
||||
}
|
||||
|
||||
|
||||
$array = [
|
||||
1 => 3,
|
||||
2.2 => 4, // Note: Floats are cast to ints, which means that the fractional part will be truncated.
|
||||
|
||||
Reference in New Issue
Block a user