2

I have two multidimensional arrays what I want to merge but, I want to skip the same values during merging. I tried several different ways what I find on the web, but nothing help me in my case.

My initial array is much bigger than this one, but here I just put an example:

$array1 = [
    "aaa" => [
        "aa1"=> [
            "key1"=> "value1",
            "key2"=> "value2",
        ],
        "bbb"=> [
            "bb1",
            "bb2",
        ],
        "ccc"=> [
            "cc1",
        ],
    ],
    "ooo" => [
        "oo1"
    ]
];

And another one, which I want to merge in the first one:

$array2 = [
    "aaa" => [
        "bbb"=> [
            "bb2",
        ],
        "ccc"=> [
            "cc2",
        ],
    ],
];

print_r of the array_merge_recurisive, using the method like: print_r(array_merge_recursive($array1, $array2));

Array
(
    [aaa] => Array
        (
            [aa1] => Array
                (
                    [key1] => value1
                    [key2] => value2
                )

            [bbb] => Array
                (
                    [0] => bb1
                    [1] => bb2
                    [2] => bb2
                )

            [ccc] => Array
                (
                    [0] => cc1
                    [1] => cc2
                )

        )

    [ooo] => Array
        (
            [0] => oo1
        )

)

So, in the result as you can see array bbb have two bb2 values, but I need just one

4
  • have you tried $arr = array_unique (array_merge_recursive($arr1, $arr2)); Commented Oct 12, 2018 at 7:22
  • check this: array_merge_recursive. notice the line: If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended. Commented Oct 12, 2018 at 7:24
  • @lovelace yes, I tried, but not help in my case. Commented Oct 12, 2018 at 7:25
  • Related: PHP - Merging two arrays into one array (also Remove Duplicates) Commented Oct 26, 2024 at 22:26

3 Answers 3

4

The following function will remove duplicate values for an array recursively (edit: only for non-associative keys):

function array_unique_recursive(array $arr) {
  if (array_keys($arr) === range(0, count($arr) - 1)) {
    $arr = array_unique($arr, SORT_REGULAR);
  }

  foreach ($arr as $key => $item) {
    if (is_array($item)) {
      $arr[$key] = array_unique_recursive($item);
    }
  }

  return $arr;
}

You can call it after array_merge_recursive.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, it is one of the solutions. I tried to do it in one step.
@Kolesar, define what you mean with "one step". Also, since this is a solution, it is acceptable. You seem to suggest you already know several solutions. In that case your question is off-topic for StackOverflow. You may want to look at CodeReview and see if your question would be on-topic there.
I tried now this method, and it does not work in case if we have some like: "bbb"=> [ "bb1", "bb2", ], "ccc"=> [ "bb1", "bb2", ], ccc will be removed in this case, because have "same array content"
@Kolesar Edited my post.
3

Use array_replace_recursive instead of array_merge_recursive.

Comments

0

You can try this

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

2 Comments

Please add an explanation on how this solves OP's issue.
If you try with arrays from the first post, you will see that this not work, too.

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.