PHP数组将值从一个数组合并到另一个数组

PHP array merge values from one array into another

很抱歉,我读了这么多的文章来理解这一点,最终弄糊涂了自己!

我有两个数组:

数组1:

1
2
3
4
5
6
7
8
9
Array (

[0] => Array ( [ID] => SI012348 [Date] => 06/01/2016 [Month] => 1 [Tier1] => 2.188875 [Tier2] => [Tier3] => [Tier4] => [Delivery] => 0 )

[1] => Array ( [ID] => SI012351 [Date] => 06/01/2016 [Month] => 1 [Tier1] => 2.139 [Tier2] => 0 [Tier3] => 0 [Tier4] => 0 [Delivery] => 0 )

[2] => Array ( [ID] => SI012387 [Date] => 14/01/2016 [Month] => 1 [Tier1] => 0.201 [Tier2] => 0 [Tier3] => 0 [Tier4] => 0 [Delivery] => 0 )

)

数组2:(包含所有带交货费用的发票)

1
2
3
4
5
6
7
8
Array (
[SI000005] => 25
[SI000010] => 15
[SI000054] => 20
[SI000069] => 0
[SI000074] => 20
[SI000076] => 16
)

我需要更新si00000x匹配的数组1,并将数组2中的值推送到数组1中的[传递]值中。

我相信这很简单,但我所做的每件事都需要一个年龄或者崩溃!

请帮助!


您可以使用这个(注意和号):

1
2
3
foreach ($arr1 as &$rec) {
    if (isset($arr2[$rec['ID']])) $rec['Delivery'] = $arr2[$rec['ID']];
}


尝试此操作(使用引用)

1
2
3
foreach ($array1 as &$a1) {
  if(isset($array1[$a1['ID']])) $a1['Delivery'] = $array1[$a1['ID']];
}


1
2
3
4
5
foreach ($array1 as &$a1) {
    if (isset($array2[$a1['ID']])) {
        $a1['Delivery'] = $array2[$a1['ID']];
    }
}