-3

Sorry I have read so many posts to make sense of this and have finally confused myself!

I have 2 arrays:

Array 1:

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 ) 

)

Array 2: (Contains all invoices with Delivery charges)

Array ( 
[SI000005] => 25 
[SI000010] => 15 
[SI000054] => 20 
[SI000069] => 0 
[SI000074] => 20 
[SI000076] => 16
)

I need to update Array 1 where SI00000x matches and push the value from Array 2 into the [Delivery] value in Array 1.

I am sure this is straightforward but everything I try either takes an age or crashes!

Please help!

Giles
  • 871
  • 1
  • 9
  • 19

3 Answers3

3

You could use this (note the ampersand):

foreach ($arr1 as &$rec) {
    if (isset($arr2[$rec['ID']])) $rec['Delivery'] = $arr2[$rec['ID']];
}
Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
1

Try this (use a reference)

foreach ($array1 as &$a1) {
  if(isset($array1[$a1['ID']])) $a1['Delivery'] = $array1[$a1['ID']];
}
JOUM
  • 239
  • 1
  • 3
  • Why should the OP "try this"? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Nov 08 '16 at 21:58
0
foreach ($array1 as &$a1) {
    if (isset($array2[$a1['ID']])) {
        $a1['Delivery'] = $array2[$a1['ID']];
    }
}
Jaime
  • 1,402
  • 7
  • 15