0

I have this code that gives back 3 arrays and I want to check for duplicates in all 3 and then keep only 1 value. But because the array is generated in a loop array_unique / array_ diff wont work.

is there any solution for this problem?

foreach ($ci AS $i) {
    if($i == 0){
        continue;
    }
    $cfsi = $cate->getCategoriesFeatureID($i);
    echo "<pre>";
    print_r($cfsi);
    echo "</pre>";
}

The result I get

Array
(
    [0] => 14
    [1] => 15
    [2] => 16
    [3] => 18
    [4] => 19
    [5] => 20
    [6] => 27
)

Array
(
    [0] => 14
    [1] => 15
    [2] => 16
    [3] => 17
    [4] => 18
    [5] => 19
    [6] => 20
    [7] => 21
    [8] => 27
    [9] => 28
)

Array
(
    [0] => 11
    [1] => 14
    [2] => 16
    [3] => 18
    [4] => 19
    [5] => 27
    [6] => 28
)

The result I'm looking for

Array
(
    [0] => 14
    [1] => 15
    [2] => 16
    [3] => 18
    [4] => 19
    [5] => 20
    [6] => 27
)

Array
(

    [0] => 17
    [1] => 21
    [2] => 28
)

Array
(

    [0] => 11
)
0

1 Answer 1

2

What you can do is store what was previously displayed and each time only report the differences from the current to this previous value (using array_diff()). Then at the end of the loop move the current value to the previous value.

$previous = [];
foreach ($ci AS $i) {
    if($i == 0){
        continue;
    }
    $cfsi = $cate->getCategoriesFeatureID($i);
    $difference = array_values(array_diff($cfsi, $previous));
    echo "<pre>";
    print_r($difference);
    echo "</pre>";
    $previous = $cfsi;
}
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.