在PHP中合并两个数组的困难

时间:2014-07-30 10:03:55

标签: php arrays loops

我有两个数组,我想将这两个数组合并为单个数组。请查看以下详细信息:

First Array:

Array
(
    [0] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
        )

    [1] => Array
        (
            [a] => 4
            [b] => 5
            [c] => 6
        )
    [2] => Array
        (
            [a] => 7
            [b] => 8
            [c] => 9
        )
)

第二阵列:

Array
(
    [0] => Array
        (
            [d] => 100

        )

    [1] => Array
        (
            [d] => 200

        )
    [2] => Array
        (
            [d] => 300

        )
)

我想要这个结果。有人知道怎么做吗?

Array
(
    [0] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
            [d] => 100
        )

    [1] => Array
        (
            [a] => 4
            [b] => 5
            [c] => 6
            [d] => 200
        )
    [2] => Array
        (
            [a] => 7
            [b] => 8
            [c] => 9
            [d] => 300
        )

    )

希望你能理解这个问题。提前谢谢。

编辑:当我使用array_merge_recursive()时,我得到这个结果:

Array
    (
    [0] => Array
        (
            [a] => 1
            [b] => 2
            [c] => 3
        )

    [1] => Array
        (
            [a] => 4
            [b] => 5
            [c] => 6
        )
    [2] => Array
        (
            [a] => 7
            [b] => 8
            [c] => 9
        )

    [3] => Array
        (
            [d] => 100

        )

    [4] => Array
        (
            [d] => 200

        )
    [5] => Array
        (
            [d] => 300

        )
)

帮助!

2 个答案:

答案 0 :(得分:3)

使用递归合并:

array_merge_recursive($array1, $array2);

答案 1 :(得分:1)

  • 循环通过辅助数组
  • 检查主数组中是否存在辅助密钥
    • 如果是,请合并它们。

代码

foreach($arr2 as $intKey => $arrElement) {
    if(array_key_exists($intKey, $arr1)) {
        $arr1[$intKey] = array_merge($arr1[$intKey], $arrElement);
    }
}

例如;

<?php

$arr1 = array( 0 => array("a" => 1,
                          "b" => 2,
                          "c" => 3
                         ),
                1 => array("a" => 1,
                        "b" => 2,
                        "c" => 3
                ),
                2 => array("a" => 1,
                        "b" => 2,
                        "c" => 3
                ),
    );

$arr2 = array( 0 => array("d" => 4
                         ),
                1 => array("d" => 4
                ),
                2 => array("d" => 4
                ),
    );


foreach($arr2 as $intKey => $arrElement) {
    if(array_key_exists($intKey, $arr1)) {
        $arr1[$intKey] = array_merge($arr1[$intKey], $arrElement);
    }
}

echo "<pre>";
print_r( $arr1 );
echo "</pre>";
输出:
> Array (
>     [0] => Array
>         (
>             [a] => 1
>             [b] => 2
>             [c] => 3
>             [d] => 4
>         )
> 
>     [1] => Array
>         (
>             [a] => 1
>             [b] => 2
>             [c] => 3
>             [d] => 4
>         )
> 
>     [2] => Array
>         (
>             [a] => 1
>             [b] => 2
>             [c] => 3
>             [d] => 4
>         )
> 
> )