How can I merge 2 different arrays with different keys based on a shared key’s value?
I am trying to keep as basic the first array.
If in the second array, shared key’s(Mark) value is the same, merge with first array.
First array:
Array
(
[0] => Array
(
[IssueDate] => 2024-01-22
[Mark] => 400001923264133
[Vat301] => 0
[Vat331] => 0
[Vat302] => 0
[Vat332] => 0
[Vat303] => 0
[Vat333] => 0
[Vat304] => 0
[Vat334] => 0
)
[1] => Array
(
[IssueDate] => 2024-01-22
[Mark] => 400001923536194
[Vat301] => 0
[Vat331] => 0
[Vat302] => 0
[Vat332] => 0
[Vat303] => 0
[Vat333] => 0
[Vat304] => 0
[Vat334] => 0
)
)
Second array:
Array
(
[0] => Array
(
[uid] => 0D7F298912F21E7934380E5728AEF6E31AB74E3F
[Mark] => 400001923264133
[vn] => 094019245
[series] => 0
[aa] => 396663852
[name] => string
(
[name] => TYPE_2_1
[value] => 2.1
)
[netValue] => 976.15
[vatAmount] => 246.50
[vatCategory] => int
(
[name] => VAT_1
[value] => 1
)
)
[1] => Array
(
[uid] => 0D7F298912F21E7934380E5728AEF6E31AB74E3F
[Mark] => 400001923536194
[vn] => 094019245
[series] => 0
[aa] => 396663852
[name] => string
(
[name] => TYPE_2_1
[value] => 2.1
)
[netValue] => 976.15
[vatAmount] => 246.50
[vatCategory] => int
(
[name] => VAT_1
[value] => 1
)
)
[2] => Array
(
[uid] => 74820E25D29945A74408F64FCBEAE229D4D386FF
[Mark] => 400001923801925
[vn] => 094019245
[series] => 0
[aa] => 396663852
[name] => string
(
[name] => TYPE_2_1
[value] => 2.1
)
[netValue] => 976.15
[vatAmount] => 246.50
[vatCategory] => int
(
[name] => VAT_1
[value] => 1
)
)
[3] => Array
(
[uid] => F218FCCF5A12CA067221D4C783ABC52B0A305ACA
[Mark] => 400001924018534
[vn] => 094019245
[series] => 0
[aa] => 396663852
[name] => string
(
[name] => TYPE_2_1
[value] => 2.1
)
[netValue] => 976.15
[vatAmount] => 246.50
[vatCategory] => int
(
[name] => VAT_1
[value] => 1
)
)
)
I want to add to first array, second array’s keys and values if key[‘Mark’] contains the same value.
Expected Output
Third array:
Array
(
[0] => Array
(
[uid] => 0D7F298912F21E7934380E5728AEF6E31AB74E3F
[vn] => 094019245
[series] => 0
[aa] => 396663852
[name] => string
(
[name] => TYPE_2_1
[value] => 2.1
)
[netValue] => 976.15
[vatAmount] => 246.50
[vatCategory] => int
(
[name] => VAT_1
[value] => 1
)
[IssueDate] => 2024-01-22
[Mark] => 400001923264133
[Vat301] => 0
[Vat331] => 0
[Vat302] => 0
[Vat332] => 0
[Vat303] => 0
[Vat333] => 0
[Vat304] => 0
[Vat334] => 0
)
[1] => Array
(
[uid] => 0D7F298912F21E7934380E5728AEF6E31AB74E3F
[Mark] => 400001923536194
[series] => 0
[aa] => 396663852
[name] => string
(
[name] => TYPE_2_1
[value] => 2.1
)
[netValue] => 976.15
[vatAmount] => 246.50
[vatCategory] => int
(
[name] => VAT_1
[value] => 1
)
[IssueDate] => 2024-01-22
[Mark] => 400001923536194
[Vat301] => 0
[Vat331] => 0
[Vat302] => 0
[Vat332] => 0
[Vat303] => 0
[Vat333] => 0
[Vat304] => 0
[Vat334] => 0
)
)
2
Answers
from here https://stackoverflow.com/a/68579169/10150897
There will be a few ways to do this, but I’ll demonstrate the use of references. Ideally, you should not perform nested loops or use more than two loops.
Code: (Demo)
If you want to avoid using references and are happy to generate a third array, you can create a lookup array from the first array then merge qualifying data from the second array with the relevant lookup element when pushing into the result array. (Demo)