Skip to content

Commit 9f423f0

Browse files
committed
[bugfix] ArrDots::search could not handle searches ending in a wildcard
1 parent 7cf0f0c commit 9f423f0

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/ArrDots.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ public static function search($array, $key, $wildcard = null)
155155
// If this segment is a wildcard
156156
if ($segment === $wildcard) {
157157
$values = [];
158-
$subKey = implode('.', $segments);
159158
foreach (array_keys($array) as $attr) {
160-
foreach (static::search($array, $attr.'.'.$subKey, $wildcard) as $attrKey => $value) {
159+
$subKey = implode('.', array_merge([$attr], $segments));
160+
foreach (static::search($array, $subKey, $wildcard) as $attrKey => $value) {
161161
$values[$pattern . $attrKey] = $value;
162162
}
163163
}

tests/Unit/ArrDotsTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function testSearch()
103103
'array3' => [
104104
['sub-array1' => [['item1' => 'item2-value'], ['item1' => 'item3-value']]],
105105
['sub-array1' => [['item1' => 'item4-value']]],
106-
]
106+
],
107107
];
108108

109109
$this->assertEquals([
@@ -112,6 +112,12 @@ public function testSearch()
112112
$this->assertEquals([
113113
'array0' => [1, 2, 3, 4]
114114
], ArrDots::search($data, 'array0'));
115+
$this->assertEquals([
116+
'array0.0' => 1,
117+
'array0.1' => 2,
118+
'array0.2' => 3,
119+
'array0.3' => 4,
120+
], ArrDots::search($data, 'array0.*', '*'));
115121
$this->assertEquals([
116122
'array2.0.sub-array0' => [1, 2, 3, 4],
117123
'array2.1.sub-array0' => [1, 2, 3, 4],

0 commit comments

Comments
 (0)