diff --git a/system/Helpers/array_helper.php b/system/Helpers/array_helper.php index b5350cdd8b70..9d2b44876cc9 100644 --- a/system/Helpers/array_helper.php +++ b/system/Helpers/array_helper.php @@ -89,8 +89,8 @@ function _array_search_dot(array $indexes, array $array) return _array_search_dot($indexes, $array[$currentIndex]); } - // Otherwise we've found our match! - return $array[$currentIndex]; + // Otherwise, not found. + return null; } } diff --git a/tests/system/Helpers/ArrayHelperTest.php b/tests/system/Helpers/ArrayHelperTest.php index 59257fbab7d6..50c6f94f4bfc 100644 --- a/tests/system/Helpers/ArrayHelperTest.php +++ b/tests/system/Helpers/ArrayHelperTest.php @@ -45,7 +45,16 @@ public function testArrayDotTooManyLevels() ], ]; - $this->assertSame(23, dot_array_search('foo.bar.baz', $data)); + $this->assertNull(dot_array_search('foo.bar.baz', $data)); + } + + public function testArrayDotTooManyLevelsWithWildCard() + { + $data = [ + 'a' => [], + ]; + + $this->assertNull(dot_array_search('a.*.c', $data)); } /** @@ -77,15 +86,24 @@ public function testArrayDotEscape() public function testArraySearchDotMultiLevels() { - $data1 = ['bar' => [['foo' => 'baz']]]; - $data2 = ['bar' => [ - ['foo' => 'bizz'], - ['foo' => 'buzz'], - ]]; - $data3 = ['baz' => 'none']; - + $data1 = [ + 'bar' => [ + ['foo' => 'baz'], + ], + ]; $this->assertSame('baz', dot_array_search('bar.*.foo', $data1)); + + $data2 = [ + 'bar' => [ + ['foo' => 'bizz'], + ['foo' => 'buzz'], + ], + ]; $this->assertSame(['bizz', 'buzz'], dot_array_search('bar.*.foo', $data2)); + + $data3 = [ + 'baz' => 'none', + ]; $this->assertNull(dot_array_search('bar.*.foo', $data3)); } diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index 32129c30c4eb..b314dd892794 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -22,6 +22,7 @@ BREAKING - The color code output by :ref:`CLI::color() ` has been changed to fix a bug. - To prevent unexpected access from the web browser, if a controller is added to a cli route (``$routes->cli()``), all methods of that controller are no longer accessible via auto-routing. - There is a possible backward compatibility break for those users extending the History Collector and they should probably update ``History::setFiles()`` method. +- The :php:func:`dot_array_search`'s unexpected behavior has been fixed. Now ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returns ``null``. The previous versions returned ``23``. Enhancements ************ diff --git a/user_guide_src/source/helpers/array_helper.rst b/user_guide_src/source/helpers/array_helper.rst index 845ccccf39b4..cf9517925234 100644 --- a/user_guide_src/source/helpers/array_helper.rst +++ b/user_guide_src/source/helpers/array_helper.rst @@ -48,6 +48,9 @@ The following functions are available: .. literalinclude:: array_helper/005.php +.. note:: Prior to v4.2.0, ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returned ``23`` + due to a bug. v4.2.0 and later returns ``null``. + .. php:function:: array_deep_search($key, array $array) :param mixed $key: The target key