0

我需要粘合几个包含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() 方法更简单/更快/更聪明的方法?

4

3 回答 3

2

很确定array_merge()应该可以正常工作。也许它们并不完全相同,但它们应该几乎相同。这可能是一种语义阻止它返回 true,为什么不将它们并排打印并找出区别呢?

也可能用这种方法代替。

于 2013-04-23T14:34:44.637 回答
2

我认为array_merge这是您正在寻找的:

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

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

于 2013-04-23T14:36:32.343 回答
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
        )

)

我想知道区别...

于 2013-04-23T15:01:20.077 回答