如何在PHP中合并这两个数组?

时间:2015-11-03 11:47:13

标签: php arrays

我有一个数组$array1,其中包含不同数量的键和值对:

Array
(
    [0] => Array
        (
            [ID] => 39
            [title] => Apple
        )

    [1] => Array
        (
            [ID] => 40
            [title] => Orange
        )

)

和另一个数组$array2

Array
(
    [0] => 273
    [1] => 386

)

我希望得到这个:

Array
(
    [0] => Array
        (
            [ID] => 39
            [title] => Apple
            [pages] => 273
        )

    [1] => Array
        (
            [ID] => 40
            [title] => Orange
            [pages] => 386
        )

)

每个数组中的项目数相同且对应,因此,我们不需要检查这个,那么,如何合并它呢?

7 个答案:

答案 0 :(得分:7)

如果要使用整数键合并,请使用array_replace_recursive;如果希望仅合并字符串键,请使用array_merge_recursive

<?php

$a1 = array(
    0 => array
    (
        "ID" => 39,
        "title" => "Apple"
    ),

    1 => array(
        "ID" => 40,
        "title" => "Orange"
    )

);

$a2 = array(
    0 => array
    (
        "pages" => 273,
        "year" => 1981
    ),

    1 => array(
        "pages" => 386,
        "year" => 1979
    )

);

$a3 = array_replace_recursive($a1, $a2);

var_dump($a3);

结果:

array(2) {
  [0] =>
  array(4) {
    'ID' =>
    int(39)
    'title' =>
    string(5) "Apple"
    'pages' =>
    int(273)
    'year' =>
    int(1981)
  }
  [1] =>
  array(4) {
    'ID' =>
    int(40)
    'title' =>
    string(6) "Orange"
    'pages' =>
    int(386)
    'year' =>
    int(1979)
  }
}

回答更新的问题:

<?php

$a1 = array(
    0 => array
    (
        "ID" => 39,
        "title" => "Apple"
    ),

    1 => array(
        "ID" => 40,
        "title" => "Orange"
    )

);

$a2 = array(
    0 => 31,
    1 => 324
);

$defaultValue = 0;
foreach ($a1 as $key => $value) {
    $a1[$key]['pages'] = isset($a2[$key]) ? $a2[$key] : $defaultValue;
}
var_dump($a1);

结果:

array(2) {
  [0] =>
  array(3) {
    'ID' =>
    int(39)
    'title' =>
    string(5) "Apple"
    'pages' =>
    int(31)
  }
  [1] =>
  array(3) {
    'ID' =>
    int(40)
    'title' =>
    string(6) "Orange"
    'pages' =>
    int(324)
  }
}

答案 1 :(得分:1)

试试这段代码: -

$newArray = array();
$i=0;
foreach($array1 as $value){
  $newArray[$i] = $value;
  $newArray[$i][] = $array2[$i];
  $i++;
}

答案 2 :(得分:1)

以下是生成您正在寻找的输出的代码:

<?php
    $list1 = array(array('id' => 39,'title' => 'Apple'),array('id' => 40,'title' => 'Orange'));
    $list2 = array(array('pages' => 273,'year' => 1981),array('pages' => 386,'year' => 1979));

    $newList = array();

    $i=0;
    foreach($list1 as $firstList){
        foreach($list2 as $secondList){
            $newList[$i] = array_merge($firstList, $secondList);
        }
    $i++;
    }

    echo"<pre>"; print_r($newList); echo"</pre>";

?>

输出:

enter image description here

答案 3 :(得分:0)

试试这个

array1[0][pages]=array2[0][pages];
array1[0][year]=array2[0][year];
array1[1][pages]=array2[1][pages];
array1[1][year]=array2[1][year];

答案 4 :(得分:0)

如果2个阵列的大小相同,你可以这样做

$arr1 = array(
    array(
        'ID' => 39,
        'title' => 'Apple'
    ),
    array(
        'ID' => 40,
        'title' => 'Orange'
    )
);

$arr2 = array(
    273,
    386
);

$merged = array();

for ($i = 0; $i < count($arr1); $i++) {
    $merged[] = array_merge($arr1[$i], array('pages' => $arr2[$i]));
}

var_dump($merged);

或者如果您不想要新阵列

for ($i = 0; $i < count($arr1); $i++) {
    $arr1[$i]['pages'] = $arr2[$i];
}

答案 5 :(得分:0)

for($i=0; $i<count($array1); ++$i){
    $array1[$i]['pages'] = $array2[$i];
}
var_dump($array1);

答案 6 :(得分:0)

$output = array();
array_walk( $array1, function( $v, $k ) use ( $array2, &$output ) {
       $output[] = array_merge( $v, $array2[$k] );
    });