-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!

3 Answers 3

3

You could use this (note the ampersand):

foreach ($arr1 as &$rec) { if (isset($arr2[$rec['ID']])) $rec['Delivery'] = $arr2[$rec['ID']]; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help! Worked a treat.
1

Try this (use a reference)

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

1 Comment

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.
0
foreach ($array1 as &$a1) { if (isset($array2[$a1['ID']])) { $a1['Delivery'] = $array2[$a1['ID']]; } } 

1 Comment

Updated. The changes will persist outside the loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.