Skip to content

Commit 36c35ff

Browse files
authored
Merge pull request #6 from stellarwp/feature/variadic-intersect-key-recursive
Support multiple arrays in intersect_key_recursive()
2 parents 2e015a9 + 48985d1 commit 36c35ff

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

docs/classes/StellarWP/Arrays/Arr.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ public static wrap(mixed $value): array
16611661
Recursively computes the intersection of arrays using keys for comparison.
16621662

16631663
```php
1664-
public static intersect_key_recursive(array $array1, array $array2): array
1664+
public static intersect_key_recursive(array $array, array $arrays): array
16651665
```
16661666

16671667

@@ -1675,13 +1675,14 @@ public static intersect_key_recursive(array $array1, array $array2): array
16751675

16761676
| Parameter | Type | Description |
16771677
|-----------|------|-------------|
1678-
| `$array1` | **array** | The array with master keys to check. |
1679-
| `$array2` | **array** | An array to compare keys against. |
1678+
| `$array` | **array** | The array with master keys to check. |
1679+
| `$arrays` | **array** | Additional arrays to compare keys against. |
16801680

16811681

16821682
**Return Value:**
16831683

1684-
An associative array containing all the entries of array1 which have keys that are present in all arguments.
1684+
An associative array containing all the entries of $array
1685+
whose keys exist in every provided array, recursively.
16851686

16861687

16871688

src/Arrays/Arr.php

+30-9
Original file line numberDiff line numberDiff line change
@@ -1422,20 +1422,41 @@ public static function wrap( $value ) {
14221422
/**
14231423
* Recursively computes the intersection of arrays using keys for comparison.
14241424
*
1425-
* @param mixed[] $array1 The array with master keys to check.
1426-
* @param mixed[] $array2 An array to compare keys against.
1425+
* @param mixed[] $array The array with master keys to check.
1426+
* @param mixed[] ...$arrays Additional arrays to compare keys against.
14271427
*
1428-
* @return mixed[] An associative array containing all the entries of array1 which have keys that are present in all arguments.
1428+
* @return mixed[] An associative array containing all the entries of $array
1429+
* whose keys exist in every provided array, recursively.
14291430
*/
1430-
public static function intersect_key_recursive( array $array1, array $array2 ): array {
1431-
$array1 = array_intersect_key( $array1, $array2 );
1431+
public static function intersect_key_recursive( array $array, array ...$arrays ): array {
1432+
$array = array_intersect_key( $array, ...$arrays );
14321433

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 ] );
1434+
foreach ( $array as $key => $value ) {
1435+
if ( ! is_array( $value ) ) {
1436+
continue;
14361437
}
1438+
1439+
$arrays_to_intersect = [];
1440+
foreach ( $arrays as $intersect_array ) {
1441+
if ( ! isset( $intersect_array[ $key ] ) ) {
1442+
unset( $array[ $key ] );
1443+
continue 2;
1444+
}
1445+
1446+
if ( ! is_array( $intersect_array[ $key ] ) ) {
1447+
continue;
1448+
}
1449+
1450+
$arrays_to_intersect[] = $intersect_array[ $key ];
1451+
}
1452+
1453+
if ( empty( $arrays_to_intersect ) ) {
1454+
continue;
1455+
}
1456+
1457+
$array[ $key ] = static::intersect_key_recursive( $value, ...$arrays_to_intersect );
14371458
}
14381459

1439-
return $array1;
1460+
return $array;
14401461
}
14411462
}

tests/wpunit/IntersectKeyRecursiveTest.php

+41-3
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,61 @@ public function test_with_multiple_arrays(): void {
198198
'a' => 1,
199199
'b' => 2,
200200
'c' => 3,
201+
'd' => [
202+
'x' => 1,
203+
'y' => [
204+
'a' => 1,
205+
'b' => 2,
206+
],
207+
'z' => [
208+
'a' => 1,
209+
'b' => 2,
210+
],
211+
],
201212
];
202213

203214
$array2 = [
204215
'a' => 10,
205216
'b' => 20,
217+
'd' => [
218+
'y' => [
219+
'a' => 1,
220+
'b' => 2,
221+
],
222+
'z' => 2,
223+
],
206224
];
207225

208226
$array3 = [
209227
'a' => 100,
210228
'c' => 300,
229+
'd' => [
230+
'x' => 1,
231+
'y' => [
232+
'a' => 1,
233+
'b' => 2,
234+
],
235+
'z' => [
236+
'a' => 1,
237+
'b' => 2,
238+
],
239+
],
211240
];
212241

213-
$result1 = Arr::intersect_key_recursive( $array1, $array2 );
214-
$result2 = Arr::intersect_key_recursive( $result1, $array3 );
242+
$result = Arr::intersect_key_recursive( $array1, $array2, $array3 );
215243

216244
$this->assertEquals( [
217245
'a' => 1,
218-
], $result2 );
246+
'd' => [
247+
'y' => [
248+
'a' => 1,
249+
'b' => 2,
250+
],
251+
'z' => [
252+
'a' => 1,
253+
'b' => 2,
254+
],
255+
],
256+
], $result );
219257
}
220258
}

0 commit comments

Comments
 (0)