1

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.

3
  • Iterate input array and add each subarray to new array, for second and each next element check if that id is used already and if it is skip adding that element. Commented May 21, 2021 at 10:23
  • But by doing this I will lose my array structure, I have to keep the order and structure as my entries are. Or I don't understand correctly... Commented May 21, 2021 at 11:54
  • 1
    No, you will not if you'll write it correctly, just you'll get new array with the same structure and order, but without unwanted values. Commented May 21, 2021 at 11:57

1 Answer 1

1

This one-minute craft doesn't qualify for answer, however there's no other option for showing sample code. Simple iteration into a new array, to keep order and structure (sample data extended for better view).

<?php

$inputArr = [
    [4, 5],
    [1, 2],
    [5, 3],
    [2, 20],
    [19, 21],
    [9, 11],

];

$outputArr = [];

foreach ($inputArr as $subArr) {

    $valueAlreadyUsed = false;

    foreach ($subArr as $valueToCheck) {
        foreach ($outputArr as $outSubArr) {
            if (in_array($valueToCheck, $outSubArr)) {
                $valueAlreadyUsed = true;
            }
        }
    }

    if (!$valueAlreadyUsed) {
        $outputArr[] = $subArr;
    }
}

print_r($outputArr);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.