mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-10-31 18:47:33 +08:00 
			
		
		
		
	Fix #20268: Minor optimisation in \yii\helpers\BaseArrayHelper::map
				
					
				
			This commit is contained in:
		 Christina Reichel
					Christina Reichel
				
			
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			 GitHub
						GitHub
					
				
			
						parent
						
							25059c4e08
						
					
				
				
					commit
					e4d5d73490
				
			| @ -9,6 +9,7 @@ Yii Framework 2 Change Log | |||||||
| - Enh #20247: Support for variadic console controller action methods (brandonkelly) | - Enh #20247: Support for variadic console controller action methods (brandonkelly) | ||||||
| - Bug #20256: Add support for dropping views in MSSQL server when running migrate/fresh (ambrozt) | - Bug #20256: Add support for dropping views in MSSQL server when running migrate/fresh (ambrozt) | ||||||
| - Enh #20248: Add support for attaching behaviors in configurations with Closure (timkelty) | - Enh #20248: Add support for attaching behaviors in configurations with Closure (timkelty) | ||||||
|  | - Enh #20268: Minor optimisation in `\yii\helpers\BaseArrayHelper::map` (chriscpty) | ||||||
|  |  | ||||||
| 2.0.51 July 18, 2024 | 2.0.51 July 18, 2024 | ||||||
| -------------------- | -------------------- | ||||||
|  | |||||||
| @ -595,6 +595,9 @@ class BaseArrayHelper | |||||||
|      */ |      */ | ||||||
|     public static function map($array, $from, $to, $group = null) |     public static function map($array, $from, $to, $group = null) | ||||||
|     { |     { | ||||||
|  |         if (is_string($from) && is_string($to) && $group === null && strpos($from, '.') === false && strpos($to, '.') === false) { | ||||||
|  |             return array_column($array, $to, $from); | ||||||
|  |         } | ||||||
|         $result = []; |         $result = []; | ||||||
|         foreach ($array as $element) { |         foreach ($array as $element) { | ||||||
|             $key = static::getValue($element, $from); |             $key = static::getValue($element, $from); | ||||||
|  | |||||||
| @ -313,9 +313,14 @@ class BaseStringHelper | |||||||
|         } |         } | ||||||
|         if ($skipEmpty) { |         if ($skipEmpty) { | ||||||
|             // Wrapped with array_values to make array keys sequential after empty values removing |             // Wrapped with array_values to make array keys sequential after empty values removing | ||||||
|             $result = array_values(array_filter($result, function ($value) { |             $result = array_values( | ||||||
|  |                 array_filter( | ||||||
|  |                     $result, | ||||||
|  |                     function ($value) { | ||||||
|                         return $value !== ''; |                         return $value !== ''; | ||||||
|             })); |                     } | ||||||
|  |                 ) | ||||||
|  |             ); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return $result; |         return $result; | ||||||
|  | |||||||
| @ -734,6 +734,57 @@ class ArrayHelperTest extends TestCase | |||||||
|                 '345' => 'ccc', |                 '345' => 'ccc', | ||||||
|             ], |             ], | ||||||
|         ], $result); |         ], $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() |     public function testKeyExists() | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user