@@ -21,8 +21,6 @@ final class DotArrayFilter
21
21
/**
22
22
* Creates a new array with only the elements specified in dot array syntax.
23
23
*
24
- * This code comes from the dot_array_search() function.
25
- *
26
24
* @param array $indexes The dot array syntax pattern to use for filtering.
27
25
* @param array $array The array to filter.
28
26
*
@@ -33,20 +31,14 @@ public static function run(array $indexes, array $array): array
33
31
$ result = [];
34
32
35
33
foreach ($ indexes as $ index ) {
36
- // See https://regex101.com/r/44Ipql/1
37
- $ segments = preg_split (
38
- '/(?<! \\\\)\./ ' ,
39
- rtrim ($ index , '* ' ),
40
- 0 ,
41
- PREG_SPLIT_NO_EMPTY
42
- );
43
-
44
- $ segments = array_map (
45
- static fn ($ key ) => str_replace ('\. ' , '. ' , $ key ),
46
- $ segments
47
- );
48
-
49
- $ result = array_replace_recursive ($ result , self ::filter ($ segments , $ array ));
34
+ $ segments = preg_split ('/(?<! \\\\)\./ ' , $ index , -1 , PREG_SPLIT_NO_EMPTY );
35
+ $ segments = array_map (static fn ($ key ) => str_replace ('\. ' , '. ' , $ key ), $ segments );
36
+
37
+ $ filteredArray = self ::filter ($ segments , $ array );
38
+
39
+ if ($ filteredArray !== []) {
40
+ $ result = array_replace_recursive ($ result , $ filteredArray );
41
+ }
50
42
}
51
43
52
44
return $ result ;
@@ -62,53 +54,54 @@ public static function run(array $indexes, array $array): array
62
54
*/
63
55
private static function filter (array $ indexes , array $ array ): array
64
56
{
65
- // If index is empty, returns empty array.
57
+ // If there are no indexes left, return an empty array
66
58
if ($ indexes === []) {
67
59
return [];
68
60
}
69
61
70
- // Grab the current index.
62
+ // Get the current index
71
63
$ currentIndex = array_shift ($ indexes );
72
64
65
+ // If the current index doesn't exist and is not a wildcard, return an empty array
73
66
if (! isset ($ array [$ currentIndex ]) && $ currentIndex !== '* ' ) {
74
67
return [];
75
68
}
76
69
77
- // Handle Wildcard (*)
70
+ // Handle the wildcard '*' at the current level
78
71
if ($ currentIndex === '* ' ) {
79
- $ answer = [];
72
+ $ result = [];
80
73
74
+ // Iterate over all keys at this level
81
75
foreach ($ array as $ key => $ value ) {
82
- if (! is_array ($ value )) {
83
- continue ;
84
- }
85
-
86
- $ result = self ::filter ($ indexes , $ value );
87
-
88
- if ($ result !== []) {
89
- $ answer [$ key ] = $ result ;
76
+ if ($ indexes === []) {
77
+ // If no indexes are left, capture the entire value
78
+ $ result [$ key ] = $ value ;
79
+ } elseif (is_array ($ value )) {
80
+ // If there are still indexes left, continue filtering recursively
81
+ $ filtered = self ::filter ($ indexes , $ value );
82
+ if ($ filtered !== []) {
83
+ $ result [$ key ] = $ filtered ;
84
+ }
90
85
}
91
86
}
92
87
93
- return $ answer ;
88
+ return $ result ;
94
89
}
95
90
96
- // If this is the last index, make sure to return it now,
97
- // and not try to recurse through things.
91
+ // If this is the last index, return the value
98
92
if ($ indexes === []) {
99
- return [$ currentIndex => $ array [$ currentIndex ]];
93
+ return [$ currentIndex => $ array [$ currentIndex ] ?? [] ];
100
94
}
101
95
102
- // Do we need to recursively filter this value?
103
- if (is_array ($ array [$ currentIndex ]) && $ array [ $ currentIndex ] !== [] ) {
104
- $ result = self ::filter ($ indexes , $ array [$ currentIndex ]);
96
+ // If the current value is an array, recursively filter it
97
+ if (is_array ($ array [$ currentIndex ])) {
98
+ $ filtered = self ::filter ($ indexes , $ array [$ currentIndex ]);
105
99
106
- if ($ result !== []) {
107
- return [$ currentIndex => $ result ];
100
+ if ($ filtered !== []) {
101
+ return [$ currentIndex => $ filtered ];
108
102
}
109
103
}
110
104
111
- // Otherwise, not found.
112
105
return [];
113
106
}
114
107
}
0 commit comments