I have an array with several pairs of objects, I need to delete the other pairs if an object is in another pair.
The order is important and I need to remove if an element is alone. In the future I can work with pairs of 3
the function I'm trying to do:
public function filterDedupped(array $couples): array
{
$deduped = [];
foreach ($couples as $couple) {
// group without runs that are already in other groups
$array = array_filter($couples, function ($run) use ($deduped) {
return !$this->array_any($deduped, function ($g) use ($run) {
return $this->array_any($g,function ($r) use ($run){
return $r === $run;
});
});
});
if (count($array) > 1) {
$deduped[] = $array;
};
}
return $deduped;
}
The problem in my function is the last comparison is false because I compare $r to $run which is an array of 2 runs.
The array_any function I'm using :
public function array_any(array $array, callable $fn): bool
{
foreach ($array as $value) {
if ($fn($value)) {
return true;
}
}
return false;
}
That's my entries :
[
[0] => Array
(
[0] => 4
[1] => 5
)
[1] => Array
(
[0] => 1
[1] => 2
)
[2] => Array
(
[0] => 5
[1] => 3
)
]
The result I want should delete the last array [2] because the object with id 5 is already used by an other pair.