Skip to content

Commit b1fdfa1

Browse files
committed
Add intersect_key_recursive + tests
1 parent 1e2a5d6 commit b1fdfa1

File tree

2 files changed

+240
-0
lines changed

2 files changed

+240
-0
lines changed

src/Arrays/Arr.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,4 +1418,24 @@ public static function wrap( $value ) {
14181418

14191419
return is_array( $value ) ? $value : [ $value ];
14201420
}
1421+
1422+
/**
1423+
* Recursively computes the intersection of arrays using keys for comparison.
1424+
*
1425+
* @param mixed[] $array1 The array with master keys to check.
1426+
* @param mixed[] $array2 An array to compare keys against.
1427+
*
1428+
* @return mixed[] An associative array containing all the entries of array1 which have keys that are present in all arguments.
1429+
*/
1430+
public static function intersect_key_recursive( array $array1, array $array2 ): array {
1431+
$array1 = array_intersect_key( $array1, $array2 );
1432+
1433+
foreach ( $array1 as $key => $value ) {
1434+
if ( is_array( $value ) && is_array( $array2[ $key ] ) ) {
1435+
$array1[ $key ] = static::intersect_key_recursive( $value, $array2[ $key ] );
1436+
}
1437+
}
1438+
1439+
return $array1;
1440+
}
14211441
}
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
<?php
2+
3+
namespace StellarWP\Arrays;
4+
5+
use StellarWP\Arrays\Tests\ArraysTestCase;
6+
7+
final class IntersectKeyRecursiveTest extends ArraysTestCase {
8+
9+
public function test_empty_arrays(): void {
10+
$result = Arr::intersect_key_recursive( [], [] );
11+
12+
$this->assertSame( [], $result );
13+
}
14+
15+
public function test_nested_empty_arrays(): void {
16+
$array1 = [
17+
'a' => [],
18+
'b' => [],
19+
];
20+
21+
$array2 = [
22+
'a' => [],
23+
'b' => [],
24+
];
25+
26+
$result = Arr::intersect_key_recursive( $array1, $array2 );
27+
28+
$this->assertSame( $array1, $result );
29+
}
30+
31+
public function test_with_simple_arrays(): void {
32+
$array1 = [
33+
'a' => 1,
34+
'b' => 22,
35+
'c' => 33,
36+
];
37+
38+
$array2 = [
39+
'a' => 1,
40+
'b' => 2,
41+
'c' => 3,
42+
];
43+
44+
$result = Arr::intersect_key_recursive( $array1, $array2 );
45+
46+
$this->assertSame( [
47+
'a' => 1,
48+
'b' => 22,
49+
'c' => 33,
50+
], $result );
51+
}
52+
53+
public function test_with_no_common_keys(): void {
54+
$array1 = [
55+
'x' => 1,
56+
'y' => 2,
57+
];
58+
59+
$array2 = [
60+
'a' => 1,
61+
'b' => 2,
62+
'c' => 3,
63+
];
64+
65+
$result = Arr::intersect_key_recursive( $array1, $array2 );
66+
67+
$this->assertSame( [], $result );
68+
}
69+
70+
public function test_nested_arrays(): void {
71+
$array1 = [
72+
'a' => 1,
73+
'b' => [
74+
'x' => 50,
75+
'y' => [
76+
'a' => 11,
77+
'b' => 10,
78+
],
79+
],
80+
'c' => 33,
81+
'd' => [
82+
1,
83+
2,
84+
],
85+
];
86+
87+
$array2 = [
88+
'a' => 1,
89+
'b' => [
90+
'x' => 5,
91+
'y' => [
92+
'a' => 1,
93+
],
94+
'z' => 10,
95+
],
96+
'c' => 2,
97+
'd' => [],
98+
];
99+
100+
$result = Arr::intersect_key_recursive( $array1, $array2 );
101+
102+
$this->assertSame([
103+
'a' => 1,
104+
'b' => [
105+
'x' => 50,
106+
'y' => [
107+
'a' => 11,
108+
],
109+
],
110+
'c' => 33,
111+
'd' => [],
112+
], $result );
113+
}
114+
115+
public function test_nested_array_with_type_overrides(): void {
116+
$array1 = [
117+
'b' => [
118+
'one',
119+
'two',
120+
'three',
121+
'four',
122+
],
123+
'c' => 'two',
124+
'd' => 100,
125+
];
126+
127+
$array2 = [
128+
'a' => 1,
129+
'b' => [
130+
1,
131+
2,
132+
3,
133+
],
134+
'c' => 2,
135+
'd' => [],
136+
];
137+
138+
$result = Arr::intersect_key_recursive( $array1, $array2 );
139+
140+
$this->assertSame( [
141+
'b' => [
142+
'one',
143+
'two',
144+
'three',
145+
],
146+
'c' => 'two',
147+
'd' => 100,
148+
], $result );
149+
}
150+
151+
public function test_nested_array_with_different_order(): void {
152+
$array1 = [
153+
'a' => 10,
154+
'b' => [
155+
'x' => 50,
156+
'y' => 60,
157+
],
158+
];
159+
160+
$array2 = [
161+
'b' => [
162+
'x' => 5,
163+
],
164+
'a' => 1,
165+
];
166+
167+
$result = Arr::intersect_key_recursive( $array1, $array2 );
168+
169+
$this->assertSame([
170+
'a' => 10,
171+
'b' => [
172+
'x' => 50,
173+
]
174+
], $result );
175+
}
176+
177+
public function test_with_all_common_keys_and_values(): void {
178+
$array1 = [
179+
'a' => 1,
180+
'b' => [
181+
'x' => 5,
182+
'y' => [
183+
'a' => 1,
184+
],
185+
'z' => 10,
186+
],
187+
'c' => 2,
188+
'd' => [],
189+
];
190+
191+
$result = Arr::intersect_key_recursive( $array1, $array1 );
192+
193+
$this->assertSame( $array1, $result );
194+
}
195+
196+
public function test_with_multiple_arrays(): void {
197+
$array1 = [
198+
'a' => 1,
199+
'b' => 2,
200+
'c' => 3,
201+
];
202+
203+
$array2 = [
204+
'a' => 10,
205+
'b' => 20,
206+
];
207+
208+
$array3 = [
209+
'a' => 100,
210+
'c' => 300,
211+
];
212+
213+
$result1 = Arr::intersect_key_recursive( $array1, $array2 );
214+
$result2 = Arr::intersect_key_recursive( $result1, $array3 );
215+
216+
$this->assertEquals( [
217+
'a' => 1,
218+
], $result2 );
219+
}
220+
}

0 commit comments

Comments
 (0)