diff --git a/docs/classes/StellarWP/Arrays/Arr.md b/docs/classes/StellarWP/Arrays/Arr.md index 2a81505..0c6e6e0 100644 --- a/docs/classes/StellarWP/Arrays/Arr.md +++ b/docs/classes/StellarWP/Arrays/Arr.md @@ -1661,7 +1661,7 @@ public static wrap(mixed $value): array Recursively computes the intersection of arrays using keys for comparison. ```php -public static intersect_key_recursive(array $array1, array $array2): array +public static intersect_key_recursive(array $array, array $arrays): array ``` @@ -1675,13 +1675,14 @@ public static intersect_key_recursive(array $array1, array $array2): array | Parameter | Type | Description | |-----------|------|-------------| -| `$array1` | **array** | The array with master keys to check. | -| `$array2` | **array** | An array to compare keys against. | +| `$array` | **array** | The array with master keys to check. | +| `$arrays` | **array** | Additional arrays to compare keys against. | **Return Value:** -An associative array containing all the entries of array1 which have keys that are present in all arguments. +An associative array containing all the entries of $array +whose keys exist in every provided array, recursively. diff --git a/src/Arrays/Arr.php b/src/Arrays/Arr.php index 8e87a5b..293fcfe 100644 --- a/src/Arrays/Arr.php +++ b/src/Arrays/Arr.php @@ -1422,20 +1422,41 @@ public static function wrap( $value ) { /** * Recursively computes the intersection of arrays using keys for comparison. * - * @param mixed[] $array1 The array with master keys to check. - * @param mixed[] $array2 An array to compare keys against. + * @param mixed[] $array The array with master keys to check. + * @param mixed[] ...$arrays Additional arrays to compare keys against. * - * @return mixed[] An associative array containing all the entries of array1 which have keys that are present in all arguments. + * @return mixed[] An associative array containing all the entries of $array + * whose keys exist in every provided array, recursively. */ - public static function intersect_key_recursive( array $array1, array $array2 ): array { - $array1 = array_intersect_key( $array1, $array2 ); + public static function intersect_key_recursive( array $array, array ...$arrays ): array { + $array = array_intersect_key( $array, ...$arrays ); - foreach ( $array1 as $key => $value ) { - if ( is_array( $value ) && is_array( $array2[ $key ] ) ) { - $array1[ $key ] = static::intersect_key_recursive( $value, $array2[ $key ] ); + foreach ( $array as $key => $value ) { + if ( ! is_array( $value ) ) { + continue; } + + $arrays_to_intersect = []; + foreach ( $arrays as $intersect_array ) { + if ( ! isset( $intersect_array[ $key ] ) ) { + unset( $array[ $key ] ); + continue 2; + } + + if ( ! is_array( $intersect_array[ $key ] ) ) { + continue; + } + + $arrays_to_intersect[] = $intersect_array[ $key ]; + } + + if ( empty( $arrays_to_intersect ) ) { + continue; + } + + $array[ $key ] = static::intersect_key_recursive( $value, ...$arrays_to_intersect ); } - return $array1; + return $array; } } diff --git a/tests/wpunit/IntersectKeyRecursiveTest.php b/tests/wpunit/IntersectKeyRecursiveTest.php index 7049973..3ba3ea8 100644 --- a/tests/wpunit/IntersectKeyRecursiveTest.php +++ b/tests/wpunit/IntersectKeyRecursiveTest.php @@ -198,23 +198,61 @@ public function test_with_multiple_arrays(): void { 'a' => 1, 'b' => 2, 'c' => 3, + 'd' => [ + 'x' => 1, + 'y' => [ + 'a' => 1, + 'b' => 2, + ], + 'z' => [ + 'a' => 1, + 'b' => 2, + ], + ], ]; $array2 = [ 'a' => 10, 'b' => 20, + 'd' => [ + 'y' => [ + 'a' => 1, + 'b' => 2, + ], + 'z' => 2, + ], ]; $array3 = [ 'a' => 100, 'c' => 300, + 'd' => [ + 'x' => 1, + 'y' => [ + 'a' => 1, + 'b' => 2, + ], + 'z' => [ + 'a' => 1, + 'b' => 2, + ], + ], ]; - $result1 = Arr::intersect_key_recursive( $array1, $array2 ); - $result2 = Arr::intersect_key_recursive( $result1, $array3 ); + $result = Arr::intersect_key_recursive( $array1, $array2, $array3 ); $this->assertEquals( [ 'a' => 1, - ], $result2 ); + 'd' => [ + 'y' => [ + 'a' => 1, + 'b' => 2, + ], + 'z' => [ + 'a' => 1, + 'b' => 2, + ], + ], + ], $result ); } }