diff --git a/system/Helpers/array_helper.php b/system/Helpers/array_helper.php index 71eac070167c..8819a2e2f058 100644 --- a/system/Helpers/array_helper.php +++ b/system/Helpers/array_helper.php @@ -26,7 +26,11 @@ */ function dot_array_search(string $index, array $array) { - $segments = explode('.', rtrim(rtrim($index, '* '), '.')); + $segments = preg_split('/(?assertEquals(23, dot_array_search('foo.bar.baz', $data)); } + public function testArrayDotEscape() + { + $data = [ + 'foo' => [ + 'bar.baz' => 23, + ], + 'foo.bar' => [ + 'baz' => 42, + ], + ]; + + $this->assertEquals(23, dot_array_search('foo.bar\.baz', $data)); + $this->assertEquals(42, dot_array_search('foo\.bar.baz', $data)); + } + public function testArrayDotReturnNullEmptyArray() { $data = []; diff --git a/user_guide_src/source/helpers/array_helper.rst b/user_guide_src/source/helpers/array_helper.rst index 096501858723..c8b77c464474 100644 --- a/user_guide_src/source/helpers/array_helper.rst +++ b/user_guide_src/source/helpers/array_helper.rst @@ -57,6 +57,23 @@ The following functions are available: // Returns: 23 $baz = dot_array_search('foo.*.baz', $data); + If the array key contains a dot, then the key can be escaped with a backslash:: + + $data = [ + 'foo' => [ + 'bar.baz' => 23 + ], + 'foo.bar' => [ + 'baz' => 43 + ], + ]; + + // Returns: 23 + $barBaz = dot_array_search('foo.bar\.baz', $data); + // Returns: 42 + $fooBar = dot_array_search('foo\.bar.baz', $data); + + .. php:function:: array_deep_search($key, array $array) :param mixed $key: The target key