0

I see many answers about the way to remove duplicate keys after comparing two arrays.

But I'm trying to merge two arrays and remove duplicates arrays (I don't want to keep unique array).

For example I would like to compare the value of [Id] key and delete the entire array if it exists in both arrays.

First array :

array(
    [8] => Array (
        [Id] => 200
        [City] => New York
        [Code] => AHSKDI
    )
    [15] => Array (
        [Id] => 340
        [City] => Paris
        [Code] => ALDLPKZAKP
    )
)

Second array :

array(
    [7] => Array (
        [Id] => 730
        [City] => Tokyo
        [Code] => AJJSJOOO
    )
    [10] => Array (
        [Id] => 200
        [City] => New York
        [Code] => AHSKDI
    )
)

I would like to remove definitively the array with [Id] => 200

array(
    [0] => Array (
        [Id] => 340
        [City] => Paris
        [Code] => ALDLPKZAKP
    )
    [1] => Array (
        [Id] => 730
        [City] => Tokyo
        [Code] => AJJSJOOO
    )
)

So first I use array_merge and then I use array_unique

array_unique(array_merge($array1, $array2), SORT_REGULAR);

But array_unique keep one of array duplicated and I don't want.

Is there another PHP function for this?

Thank you in advance

Jandon
  • 605
  • 7
  • 32
  • Maybe you can find your answer here [Stackoverflow - PHP merge arrays with only NOT DUPLICATE values](https://stackoverflow.com/questions/10572546/php-merge-arrays-with-only-not-duplicated-values) – CooleKikker2 Oct 13 '21 at 12:04
  • Check the manual for [`array_unique()`](https://www.php.net/manual/en/function.array-unique.php) to see why that function was not designed to be used on this type of array. But the comments contain some work that may help you – RiggsFolly Oct 13 '21 at 12:05
  • Thank you @CooleKikker2. I did exactly the same : `array_unique (array_merge ($array1, $array2));` But I got unique array – Jandon Oct 13 '21 at 12:06

0 Answers0