3

我有以下两个数组:

$Array1 = array(array('x1','x2','x3'),array('x4','x5','x6'));
$Array2 = array(array('z1','z2','z3'),array('z4','z5','z6'));

打印时它们返回:

Array
(
    [0] => Array
        (
            [x1] => x1
            [x2] => x2
            [x3] => x3
        )

    [1] => Array
        (
            [x4] => x4
            [x5] => x5
            [x6] => x6
        )

)

Array
(
    [0] => Array
        (
            [z1] => z1
            [z2] => z2
            [z3] => z3
        )

    [1] => Array
        (
            [z4] => z4
            [z5] => z5
            [z6] => z6
        )

)

我如何将它们组合成一个数组,如下例所示,它们使用相同的键组合:

Array
(
    [0] => Array
        (
            [x1] => x1
            [x2] => x2
            [x3] => x3
            [z1] => z1
            [z2] => z2
            [z3] => z3
        )

    [1] => Array
        (
            [x4] => x4
            [x5] => x5
            [x6] => x6
            [z1] => z4
            [z2] => z5
            [z3] => z6
        )

)

我试过使用:

array_merge=($Array1,$Array2) 

array_merge_recursive($Array1,$Array2) 

array_combine($Array1,$Array2) [which genereated an error, so I guess I was using it wrong]

$Array1 + $Array2

但这些都没有奏效。

4

6 回答 6

5

像这样的东西应该工作:

$output = array();
foreach ($Array1 as $key => $arr) {
  $output[] = array_merge($arr, $Array2[$key]);
}
于 2013-07-31T12:49:50.647 回答
2

您必须记住,您是在多维数组上使用这些函数。你在正确的轨道上使用 array_merge 但你必须在你的数组的第二级调用它,你将不得不迭代第一级

因此,在这些数组中的每个二级元素上调用array_merge应该可以工作:

$Array1 = array(array('x1','x2','x3'),array('x4','x5','x6'));
$Array2 = array(array('z1','z2','z3'),array('z4','z5','z6'));
$output = array();

foreach ($Array1 as $key=>$value) {
    $output[$key] = array_merge($value, $Array2[$key]);
    // same as:
    //$output[$key] = array_merge($Array1[$key], $Array2[$key]);
}

您将在$output中获得最终值

于 2013-07-31T12:51:17.280 回答
1

尝试

$Array1 = array_merge(array('x1','x2','x3'),array('x4','x5','x6'));
$Array2 = array_merge(array('z1','z2','z3'),array('z4','z5','z6'));

$Array = array_merge($Array1,$Array2)

文档在这里

于 2013-07-31T12:49:56.143 回答
1

试试这个

$arr1 = array_merge($Array1[0],$Array2[0]);
$arr2 = array_merge($Array1[1],$Array2[1]);
$array = array($arr1, $arr2);
于 2013-07-31T12:50:47.193 回答
1

即使您没有数字索引,这也有效:

$Array1 = array(array('x1','x2','x3'),array('x4','x5','x6'));
$Array2 = array(array('z1','z2','z3'),array('z4','z5','z6'));

$new_array = array();

foreach($Array1 as $key => $val) {
    if(array_key_exists($key, $Array2)) {
        $new_array[$key] = array_merge($val, $Array2[$key]);
    }
}

echo '<pre>';
print_r($new_array);
echo '</pre>';
于 2013-07-31T12:54:46.620 回答
0

试试这个。

<?php
$Array1 = array(array('x1' => 'x1', 'x2' => 'x2', 'x3' => 'x3'),
    array('x4' =>'x4','x5' =>'x5','x6' =>'x6'));


$Array2 = array(array('z1' =>'z1', 'z2' =>'z2', 'z3' =>'z3'),
array('z4' =>'z4','z5' =>'z5', 'z6' =>'z6'));


$final=array();
foreach($Array1 as $key1=>$arr1)
{
 foreach($Array2 as $key2=>$arr2)
  {
    if($key1==$key2)
    {
        $sub=array_merge($arr1,$arr2);
        $final[]=$sub;
    }
   }  
}

echo '<pre>';
print_r($final);
echo '</pre>';

?>

你可以在这里查看工作演示(http://codepad.org/1Eu9vjah

于 2013-07-31T13:18:49.990 回答