Let's say I have following arrays:
Array
(
[0] => Array
(
[id] => 5
[name] => Education
)
[1] => Array
(
[id] => 4
[name] => Computers
)
[3] => Array
(
[id] => 7
[name] => Science
[4] => Array
(
[id] => 1
[name] => Sports
)
)
And the second one:
Array
(
[0] => Array
(
[id] => 1
[title] => Sport
)
[1] => Array
(
[id] => 7
[title] => Sci
)
[3] => Array
(
[id] => 4
[title] => Comp
[4] => Array
(
[id] => 5
[title] => Edu
)
)
And desired output is:
Array
(
[0] => Array
(
[id] => 5
[name] => Education
[title] => Edu
)
[1] => Array
(
[id] => 4
[name] => Computers
[title] => Comp
)
[3] => Array
(
[id] => 7
[name] => Science
[title] => Sci
[4] => Array
(
[id] => 1
[name] => Sports
[title] => Sport
)
)
I have managed to merge these arrays with simply:
foreach($first as $key => $value){
$result[$key] = array_merge($first[$key], $second[$key]);
}
But the output is not combined correctly:
Array
(
[0] => Array
(
[id] => 5
[name] => Education
[title] => Sport
)
[1] => Array
(
[id] => 4
[name] => Computers
[title] => Sci
)
[3] => Array
(
[id] => 7
[name] => Science
[title] => Comp
[4] => Array
(
[id] => 1
[name] => Sports
[title] => Edu
)
)
The problem is I would like to merge these arrays on the same id
.
Desired output sorting should be same as in the first array.
How can I achieve this? Any help is much appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…