合并2个没有重复键的数组

时间:2010-11-09 18:28:12

标签: php arrays

我有2个如下所示的数组,并希望将它们合并在一起,而不是将重复键复制到新数组中。

$ 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
                    )
            )
    )
)

$数组2:

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);

$结果:

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
                    )
            )
    )
)

我怎样才能让它看起来像这样?

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
            )
    )
)

我是以错误的方式解决这个问题还是有办法清理它?示例欢迎!

谢谢

2 个答案:

答案 0 :(得分:3)

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;
}

答案 1 :(得分:0)

您是否尝试过array_merge页面中的用户自定义示例/功能? http://php.net/manual/en/function.array-merge.php

似乎有很多例子可能适合你的账单。保留密钥(无重新编号)的一个建议是使用+运算符

$result = $x1 + $x2(其中x是数组)