dark
dark

Reputation: 77

Merge 2 arrays with no duplicated keys

I have 2 arrays like below and want to merge them together and not duplicate keys into a new array.

$array1:

Array
(
[0] => Array
    (
        [a] => Array
            (
                [p] => Array
                    (
                        [a] => 1
                    )

            )

        [a/s] => Array
            (
                [p] => Array
                    (
                        [o] => 1
                    )

            )

        [a/p] => Array
            (
                [p] => Array
                    (
                        [w] => 1
                        [e] => 1
                        [d] => 1
                    )

            )

        [a/u] => Array
            (
                [p] => Array
                    (
                        [w] => 1
                        [e_a] => 1
                        [d_a] => 1
                    )
            )
    )
)

$array2:

Array
(
[0] => Array
    (
        [a/x] => Array
            (
                [p] => Array
                    (
                        [a] => 1
                    )

            )

        [a/s] => Array
            (
                [p] => Array
                    (
                        [o] => 1
                    )

            )

        [a/p] => Array
            (
                [p] => Array
                    (
                        [w] => 1
                        [e] => 1
                        [d] => 1
                    )

            )

        [a/u] => Array
            (
                [p] => Array
                    (
                        [w] => 1
                        [e_a] => 1
                        [d_a] => 1
                    )
            )
    )
)


$result = array_merge_recursive($array1, $array2);

$result:

Array
(
[0] => Array
    (
        [a] => Array
            (
                [p] => Array
                    (
                        [a] => 1
                    )
            )

        [a/s] => Array
            (
                [p] => Array
                    (
                        [o] => Array
                            (
                                [0] => 1
                                [1] => 1
                            )
                    )
            )

        [a/p] => Array
            (
                [p] => Array
                    (
                        [w] => Array
                            (
                                [0] => 1
                                [1] => 1
                            )

                        [e] => Array
                            (
                                [0] => 1
                                [1] => 1
                            )

                        [d] => Array
                            (
                                [0] => 1
                                [1] => 1
                            )
                    )
            )

        [a/u] => Array
            (
                [p] => Array
                    (
                        [w] => Array
                            (
                                [0] => 1
                                [1] => 1
                            )

                        [e_a] => Array
                            (
                                [0] => 1
                                [1] => 1
                            )

                        [d_a] => Array
                            (
                                [0] => 1
                                [1] => 1
                            )
                    )
            )

        [a/x] => Array
            (
                [p] => Array
                    (
                        [a] => 1
                    )
            )
    )
)

How would I go about making it look like this?

Array
(
[0] => Array
    (
        [a] => Array
            (
                [p] => Array
                    (
                        [a] => 1
                    )

            )

        [a/s] => Array
            (
                [p] => Array
                    (
                        [o] => 1

                    )

            )

        [a/p] => Array
            (
                [p] => Array
                    (
                        [w] => 1
                        [e] => 1
                        [d] => 1
                    )

            )

        [a/u] => Array
            (
                [p] => Array
                    (
                        [w] => 1
                        [e_a] => 1
                        [d_a] => 1
                    )
            )

        [a/x] => Array
            (
                [p] => 1
            )
    )
)

Am I going about this the wrong way or is there some way to clean this up? Examples welcome!

Thanks

Upvotes: 2

Views: 2721

Answers (2)

Russell Davis
Russell Davis

Reputation: 8528

function array_merge_recursive_unique($array1, $array2) {
  if (empty($array1)) return $array2; //optimize the base case

  foreach ($array2 as $key => $value) {
    if (is_array($value) && is_array(@$array1[$key])) {
      $value = array_merge_recursive_unique($array1[$key], $value);
    }
    $array1[$key] = $value;
  }
  return $array1;
}

Upvotes: 3

Jakub
Jakub

Reputation: 20475

Have you tried user custom examples/functions from the array_merge page? http://php.net/manual/en/function.array-merge.php

There seem to be quite a few examples that might fit your bill. One suggestion for keeping keys (no renumbering) is to use the + operator

$result = $x1 + $x2 (where x's are arrays)

Upvotes: -1

Related Questions