diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 35ee457cc4..c8bac20b8c 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -31,6 +31,7 @@ Yii Framework 2 Change Log - Bug #11723: Fixed PHP 7 + XDebug error handling displaying "Expected array for frame 0" (tanakahisateru) - Bug #11735: Fixed `yii\web\UploadedFile` to return `null` when there's no file uploaded (brummm) - Bug #11774: Fixed incorrect recusuive symlinks check in FileHelper (AnikanovD) +- Bug #11739: Fixed `ArrayHelper::index()` losing precision for float keys (AnikanovD) 2.0.8 April 28, 2016 -------------------- diff --git a/framework/helpers/BaseArrayHelper.php b/framework/helpers/BaseArrayHelper.php index 480cd139bf..225479372e 100644 --- a/framework/helpers/BaseArrayHelper.php +++ b/framework/helpers/BaseArrayHelper.php @@ -363,6 +363,9 @@ class BaseArrayHelper } else { $value = static::getValue($element, $key); if ($value !== null) { + if (is_float($value)) { + $value = (string) $value; + } $lastArray[$value] = $element; } } diff --git a/tests/framework/helpers/ArrayHelperTest.php b/tests/framework/helpers/ArrayHelperTest.php index a524f19094..fde429d124 100644 --- a/tests/framework/helpers/ArrayHelperTest.php +++ b/tests/framework/helpers/ArrayHelperTest.php @@ -424,6 +424,30 @@ class ArrayHelperTest extends TestCase $this->assertEquals($expected, $result); } + /** + * @see https://github.com/yiisoft/yii2/issues/11739 + */ + public function testIndexFloat() + { + $array = [ + ['id' => 1e6], + ['id' => 1e32], + ['id' => 1e64], + ['id' => 1465540807.522109], + ]; + + $expected = [ + '1000000' => ['id' => 1e6], + '1.0E+32' => ['id' => 1e32], + '1.0E+64' => ['id' => 1e64], + '1465540807.5221' => ['id' => 1465540807.522109], + ]; + + $result = ArrayHelper::index($array, 'id'); + + $this->assertEquals($expected, $result); + } + public function testGetColumn() { $array = [