我需要将两个数组合并到一个数组中。
看起来array_merge
不支持多维数组。
课程
Array (
[0] => Array ( [Name] => Course1 [CourseId] => 1 [Selected] => )
[1] => Array ( [Name] => Course2 [CourseId] => 2 [Selected] => )
[2] => Array ( [Name] => Course3 [CourseId] => 3 [Selected] => )
[3] => Array ( [Name] => Course4 [CourseId] => 4 [Selected] => )
[4] => Array ( [Name] => Course5 [CourseId] => 5 [Selected] => )
)
TeacherCourses
Array (
[0] => Array ( [CourseId] => 1 [Selected] => selected)
[1] => Array ( [CourseId] => 2 [Selected] => selected)
[2] => Array ( [CourseId] => 3 [Selected] => selected)
)
我需要这个结果
Array (
[0] => Array ( [Name] => Course1 [CourseId] => 1 [Selected] => selected)
[1] => Array ( [Name] => Course2 [CourseId] => 2 [Selected] => selected)
[2] => Array ( [Name] => Course3 [CourseId] => 3 [Selected] => selected)
[3] => Array ( [Name] => Course4 [CourseId] => 4 [Selected] => )
[4] => Array ( [Name] => Course5 [CourseId] => 5 [Selected] => )
)
答案 0 :(得分:0)
您是否尝试过 foreach 循环?
我们走了,:
foreach($arr1 as $key => $value){
foreach($arr2 as $value2){
if($value['CourseId'] === $value2['CourseId']){
$arr1[$key]['Selected'] = $value2['Selected'];
}
}
}
希望这会对你有所帮助。
继续编码:)