Skip to content

Commit 7e35125

Browse files
committed
Added intersectObjects method
1 parent cab8223 commit 7e35125

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Here, you can quickly get started by becoming familiar with each and every metho
7070
* Computations
7171
* [sum](#sumarray-arrays-array)
7272
* [diffObjects](#diffobjectsarray-array1-array-array2-array-arrays-array)
73+
* [intersectObjects](#intersectobjectsarray-array1-array-array2-array-arrays-array)
7374
* Flattening
7475
* [flatten](#flattenarray-array-int-depth--null-bool-assoc--false-array)
7576
* [flattenSingle](#flattensinglearray-array-array)
@@ -611,7 +612,7 @@ Arr::sum([null, '', false], ['1', true, 'test']) -> [1, 1, 0]
611612
```
612613

613614
### `diffObjects(array $array1, array $array2, array ...$arrays): array`
614-
Differentiate two or more arrays of objects
615+
Compute difference between two or more arrays of objects
615616

616617
```php
617618
$object1 = new \stdClass();
@@ -625,6 +626,21 @@ Arr::diffObjects([$object3, $object1, $object2], [$object3], [$object1, $object2
625626
Arr::diffObjects([$object1], [$object3], [$object2], []) -> [$object1]
626627
```
627628

629+
### `intersectObjects(array $array1, array $array2, array ...$arrays): array`
630+
Compute intersection between two or more arrays of objects
631+
632+
```php
633+
$object1 = new \stdClass();
634+
$object2 = new \stdClass();
635+
$object3 = new \stdClass();
636+
637+
Arr::intersectObjects([$object3, $object1, $object2], [$object3, $object2], [$object2]) -> [2 => $object2]
638+
639+
Arr::intersectObjects([$object3, $object1, $object2], [$object3], [$object1, $object2]) -> []
640+
641+
Arr::intersectObjects([$object1, $object2, $object3, $object1], [$object1, $object2]) -> [$object1, $object2, 3 => $object1]
642+
```
643+
628644
## Flattening
629645

630646
### `flatten(array $array, ?int $depth = null, bool $assoc = false): array`

src/Arr.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ public static function sum(array ...$arrays): array
607607
}
608608

609609
/**
610-
* Differentiate two or more arrays of objects
610+
* Compute difference between two or more arrays of objects
611611
*
612612
* @param array $array1
613613
* @param array $array2
@@ -625,6 +625,25 @@ public static function diffObjects(array $array1, array $array2, array ...$array
625625
return array_udiff(...$arguments);
626626
}
627627

628+
/**
629+
* Compute intersection between two or more arrays of objects
630+
*
631+
* @param array $array1
632+
* @param array $array2
633+
* @param array[] $arrays
634+
* @return array
635+
*/
636+
public static function intersectObjects(array $array1, array $array2, array ...$arrays): array
637+
{
638+
$arguments = $arrays;
639+
array_unshift($arguments, $array1, $array2);
640+
array_push($arguments, function ($obj1, $obj2) {
641+
return strcmp(spl_object_hash($obj1), spl_object_hash($obj2));
642+
});
643+
644+
return array_uintersect(...$arguments);
645+
}
646+
628647
/**
629648
* Flatten array of arrays to a n-depth array
630649
*

test/ArrTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,17 @@ public function testDiffObjects()
685685
$this->assertSame([$object1], Arr::diffObjects([$object1], [$object3], [$object2], []));
686686
}
687687

688+
public function testIntersectObjects()
689+
{
690+
$object1 = new stdClass();
691+
$object2 = new stdClass();
692+
$object3 = new stdClass();
693+
694+
$this->assertSame([2 => $object2], Arr::intersectObjects([$object3, $object1, $object2], [$object3, $object2], [$object2]));
695+
$this->assertSame([], Arr::intersectObjects([$object3, $object1, $object2], [$object3], [$object1, $object2]));
696+
$this->assertSame([$object1, $object2, 3 => $object1], Arr::intersectObjects([$object1, $object2, $object3, $object1], [$object1, $object2]));
697+
}
698+
688699
public function testFlatten()
689700
{
690701
$array = [

0 commit comments

Comments
 (0)