Skip to content

Support multiple arrays in intersect_key_recursive() #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/classes/StellarWP/Arrays/Arr.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand All @@ -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.



Expand Down
39 changes: 30 additions & 9 deletions src/Arrays/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
44 changes: 41 additions & 3 deletions tests/wpunit/IntersectKeyRecursiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Copy link
Contributor

@defunctl defunctl Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty this test result should actually be:

			'a' => 1,
			'd' => [
				'y' => [
					'a' => 1,
					'b' => 2,
				],
				'z' => [
					'a' => 1,
					'b' => 2,
				],
			],

'y' => [
'a' => 1,
'b' => 2,
],
'z' => [
'a' => 1,
'b' => 2,
],
],
], $result );
}
}
Loading