问题
How can I achieve the third array, merging Array1 and Array2? What's the best way to do that in PHP? Many thanks. Array2 has like index (key), the associative value of id in Array1.
Array1
Array
(
[0] => Array
(
[id] => 56
[grade] => 6.7
)
[1] => Array
(
[id] => 214
[grade] => 3.2
)
)
Array2
Array
(
[56] => 2.4
[214] => 5.8
)
Result wanted
Array
(
[0] => Array
(
[id] => 56
[grade] => 2.4
)
[1] => Array
(
[id] => 214
[grade] => 5.8
)
)
回答1:
foreach($array1 as &$arrayItem) {
$arrayItem['grade'] = $array2[$arrayItem['id']]
}
Here you will have array 1 merged as you wished
来源:https://stackoverflow.com/questions/33741689/array-and-associative-array-merge