PHP将一个多维数组附加到另​​一个多维数组的最佳方法是什么?

时间:2013-04-23 14:32:13

标签: php multidimensional-array

我需要粘贴几个包含n个关联数组的索引数组(如下所示)。我不关心外部数组的索引,我只关心内部数组的键。我已经尝试了几种方法,并且(令人惊讶地)只有其中一种 - 最丑陋 - 实际上有效。所以我开始怀疑是否有一些我缺少的聪明/快速的东西。

PHP版本:5.3+(如果重要)

这就是我所拥有的:

$arrayA = array(
        array(
            'foo' => 1,
            'bar' => 2,
            'baz' => 3
        ),
        array(
            'foo' => 12,
            'bar' => 22,
            'baz' => 32
        ),
    );

    $arrayB = array(
        array(
            'foo' => 21,
            'bar' => 22,
            'baz' => 23
        ),
        array(
            'foo' => 212,
            'bar' => 222,
            'baz' => 232
        ),
    );

这就是我想要的:

    $arrayC = array(
        array(
            'foo' => 1,
            'bar' => 2,
            'baz' => 3
        ),
        array(
            'foo' => 12,
            'bar' => 22,
            'baz' => 32
        ),
        array(
            'foo' => 21,
            'bar' => 22,
            'baz' => 23
        ),
        array(
            'foo' => 212,
            'bar' => 222,
            'baz' => 232
        ),
    );

这就是我尝试过的:

    $arrayD = $arrayA;
    foreach($arrayB as $value)
    {
        $arrayD[] = $value;
    }

    $arrayE = array_push($arrayA, $arrayB);
    $arrayF = $arrayA + $arrayB;
    $arrayG = array_merge($arrayA, $arrayB);

    print_r($arrayC == $arrayD); //TRUE - it works
    print_r($arrayC == $arrayE); //FALSE
    print_r($arrayC == $arrayF); //FALSE
    print_r($arrayC == $arrayG); //FALSE

为什么我认为array_merge($arrayA, $arrayB)不起作用

    $arrayD = $arrayA;
    foreach($arrayB as $value)
    {
        $arrayD[] = $value;
    }

    $arrayE = array_push($arrayA, $arrayB); // <-- after this line $arrayA is already broken
    $arrayF = $arrayA + $arrayB;
    $arrayG = array_merge($arrayA, $arrayB); // because $arrayA was broken I thought it didn't work

是否有比foreach()方法更简单/更快/更聪明的方法?

3 个答案:

答案 0 :(得分:2)

非常确定array_merge()应该运作良好。也许它们并不完全相同,但它们应该几乎完全相同。它可能是一种阻止它返回真实的语义,为什么不将它们并排打印并找出差异呢?

也可能是this method

答案 1 :(得分:2)

我认为array_merge正是您所寻找的:

 $arrayC = array_merge($arrayA, $arrayB);

请参阅此处的工作示例:http://codepad.org/IJ8UM6Uz

答案 2 :(得分:2)

我不确定为什么OP说array_merge不起作用:

<?
$arrayA = array(
        array(
            'foo' => 1,
            'bar' => 2,
            'baz' => 3
        ),
        array(
            'foo' => 12,
            'bar' => 22,
            'baz' => 32
        ),
    );

    $arrayB = array(
        array(
            'foo' => 21,
            'bar' => 22,
            'baz' => 23
        ),
        array(
            'foo' => 212,
            'bar' => 222,
            'baz' => 232
        ),
    );

//using ARRAY_MERGE
echo "<pre>";
$arrayC = array_merge($arrayA,$arrayB);
echo "ArrayC<br>";
print_r($arrayC); 

//using OP's method
$arrayD = $arrayA;
    foreach($arrayB as $value)
    {
        $arrayD[] = $value;
    }
echo "ArrayD<br>";
print_r($arrayD); 
?>

结果:

ArrayC //array_merge
Array
(
    [0] => Array
        (
            [foo] => 1
            [bar] => 2
            [baz] => 3
        )

    [1] => Array
        (
            [foo] => 12
            [bar] => 22
            [baz] => 32
        )

    [2] => Array
        (
            [foo] => 21
            [bar] => 22
            [baz] => 23
        )

    [3] => Array
        (
            [foo] => 212
            [bar] => 222
            [baz] => 232
        )

)

ArrayD //OP's method..
Array
(
    [0] => Array
        (
            [foo] => 1
            [bar] => 2
            [baz] => 3
        )

    [1] => Array
        (
            [foo] => 12
            [bar] => 22
            [baz] => 32
        )

    [2] => Array
        (
            [foo] => 21
            [bar] => 22
            [baz] => 23
        )

    [3] => Array
        (
            [foo] => 212
            [bar] => 222
            [baz] => 232
        )

)

我想知道其中的差异......