当键与另一个数组匹配时,关联数组中的值更新

时间:2017-09-20 05:37:12

标签: javascript php arrays multidimensional-array

我正在尝试在与 array2匹配时更新 array1 值。并且返回具有更新值的整个array1

$array1 =array("subj1"=>"10","subj2"=>"20","subj3"=>"30","subj4"=>"40","subj5"=>"36","subj6"=>"85");
print_r($array1);
Array
(
    [subj1] => 10
    [subj2] => 20
    [subj3] => 30
    [subj4] => 40
    [subj5] => 36
    [subj6] => 85
)
$array2=array("subj1"=>"110","subj2"=>"130");
print_r($array2);
Array
(
    [subj1] => 110
    [subj2] => 130
)

下面是我想要的结果。

Array
(
    [subj1] => 110
    [subj2] => 130
    [subj3] => 30
    [subj4] => 40
    [subj5] => 36
    [subj6] => 85
)

我如何实现这一目标?

2 个答案:

答案 0 :(得分:1)

您可以像这样使用array_merge

array_merge($array1, $array2)

答案 1 :(得分:1)

您可以使用以下代码获取所需的输出:

  <?php
$array1 =array("subj1"=>"10","subj2"=>"20","subj3"=>"30","subj4"=>"40","subj5"=>"36","subj6"=>"85");
print_r($array1);
echo "<br>";
$array2=array("subj1"=>"110","subj2"=>"130");
print_r($array2);
echo "<br>";
print_r(array_merge($array1, $array2)); 
?>