I have 5 multidimensional arrays that I want to merge together. All the different arrays have different lenght and each of the elements of the arrays have 4 keys and values, 3 of them are shared between the arrays. These 3 can be compared, and in the case that these 3 values match, then the arrays can be merged together.
As an example 3 of my arrays look like this:
array ( 0 => array("AccountID" => 9407, "year" => 2019,"month" => "November","received" => 36), 1 => array("AccountID" => 1975,"year" => 2019,"month" => "November","received" => 230),)
array ( 0 => array("AccountID" => 9407,"year" => 2019,"month" => "November","analyzed" => 138), 1 => array("AccountID" => 1975,"year" => 2019,"month" => "April","analyzed" => 1),)
array ( 0 => array("AccountID" => 9407,"year" => 2019,"month" => "November","failed" => 1), 1 => array("AccountID" => 1975,"year" => 2020,"month" => "April","failed" => 7),)
What I'm loooking for is something like this
^ array:11 [▼
0 => array:4 [▼
"AccountID" => 9407
"year" => 2019
"month" => "November"
"received" => 36
"analyzed" => 138
"failed" => 1
]
1 => array:4 [▼
"AccountID" => 1975
"year" => 2020
"month" => "April"
"received" => 230
"analyzed" => 1
"failed" => 7 ]
until now I've tried the following code, which I use to merge 2 arrays together. this i repeat for the other arrays, merging 2 at once. But it is delivering wrong values. Probably because it is only comparing if the key exist and not also the value of it.
foreach ($received_list as $key => $received) {
if (array_key_exists($key, $analyzed_list)) {
$merged[$key] = array_merge($received_list[$key], $analyzed_list[$key]);
} else {
$merged[$key] = array_merge($received_list[$key], ['analyzed' => 0]);
}
}
foreach ($analyzed_list as $key => $received) {
if (array_key_exists($key, $received_list)) {
$merged[$key] = array_merge($analyzed_list[$key], $received_list[$key]);
} else {
$merged[$key] = array_merge($analyzed_list[$key], ['received' => 0]);
}
}
Any kind of help is much appreciated!