- map操作
模拟数据:
//返回数据
$outputData = [
// [
// 'id' => '',
// 'studentName' => '',
// 'score' => '',
// ],
];
//模拟学生表数据
$mockStudentData = [
[
'id' => 1,
'name' => '张三',
],
[
'id' => 2,
'name' => '李四',
],
[
'id' => 3,
'name' => '王五',
],
];
//模拟学生成绩表数据
$mockStudentScoreData = [
[
'id' => 1,
'studentID' => 1,
'score' => 90,
],
[
'id' => 2,
'studentID' => 2,
'score' => 90,
],
[
'id' => 3,
'studentID' => 3,
'score' => 90,
],
]; 操作:
//定义map数组
$mapStudent = [];
foreach($mockStudentData as $mockStudentValue){
//以id为下标
$mapStudent[$mockStudentValue['id']] = $mockStudentValue['name'];
}
foreach($mockStudentScoreData as $mockStudentScoreValue){
$item = [
'studentScoreID' => $mockStudentScoreValue['id'],
'score' => $mockStudentScoreValue['score'],
'studentName' => $mapStudent[$mockStudentScoreValue['studentID']],
];
$outputData[] = $item;
}
return $outputData;上面例子student为主表,studentScore为从表,通过主表的主键做map的key(核心点也就是两个数据的关联点做key),遍历从表数据,进行匹配到了map中的key对应的就是他的父级数据。
当前例子只是简单的map操作,具体业务场景会遇到比较复杂点的情况,但万变不离其宗,稍微设计改变map数据结构一样能解决问题。