This is not a duplicate of the following questions:
array merge with same key in php
PHP Merge array with same keys and one same value
PHP array merge elements with same key
Say I have two arrays:
$A = [
0 => [
'id' => 10001,
'name' => 'John'
],
1 => [
'id' => 10002,
'name' => 'Jack'
]
];
$B = [
0 => [
'id' => 10001,
'count' => 4
],
2 => [
'id' => 10002,
'count' => 6
]
];
What I'm expecting result after merging is like this:
$C = [
1 => [
'id' => 10001,
'name' => 'John',
'count' => 4
],
2 => [
'id' => 10002,
'name' => 'Jack',
'count' => 6
]
];
Right now what I'm doing is loop two arrays, find the same key, and add the 'count' after to 'name'. It works, but since these two arrays are big, there may be another way that is more efficient and elegant.
Any suggestion would be appreciated!