ideas:
- array_walk array
- Group according to teacher_id field
- then student is grouped into the previous array subarray under the student_id field
Code
$data = [
[
'id' => '2',
'one_day' => '2018-09-30',
'teacher_id' => '1',
'teacher_name' => '',
'student_id' => '8',
'student_name' => '',
'form_done_id' => '13',
],
[
'id' => '2',
'one_day' => '2018-09-30',
'teacher_id' => '1',
'teacher_name' => '',
'student_id' => '14',
'student_name' => '',
'form_done_id' => '14',
]
];
$result = [];
array_walk($data, function($v, $i) use (&$result) {
if (!isset($result[$v['id']])) {
$result[$v['id']] = [
'id' => $v['id'],
'one_day' => $v['one_day'],
'teacher_id' => $v['teacher_id'],
'teacher_name' => $v['teacher_name'],
];
}
$result[$v['id']]['student_list'][] = [
'student_id' => $v['student_id'],
'student_name' => $v['student_name'],
'form_done_id' => $v['form_done_id'],
];
});
$result = array_values($result);
var_dump($result);
< H1 > output < / H1 >
array(1) {
[0]=>
array(5) {
["id"]=>
string(1) "2"
["one_day"]=>
string(10) "2018-09-30"
["teacher_id"]=>
string(1) "1"
["teacher_name"]=>
string(9) ""
["student_list"]=>
array(2) {
[0]=>
array(3) {
["student_id"]=>
string(1) "8"
["student_name"]=>
string(6) ""
["form_done_id"]=>
string(2) "13"
}
[1]=>
array(3) {
["student_id"]=>
string(2) "14"
["student_name"]=>
string(6) ""
["form_done_id"]=>
string(2) "14"
}
}
}
}
$common = array_intersect($data[0],$data[1]); //
$diff1 = array_diff($data[0],$common);//
$diff2 = array_diff($data[1],$common);
$common['student_list']=[$diff1,$diff2];
var_dump($common);
$data = [
]
[
'id' => '2',
'one_day' => '2018-09-30',
'teacher_id' => '1',
'teacher_name' => '',
'student_id' => '8',
'student_name' => '',
'form_done_id' => '13',
],
[
'id' => '2',
'one_day' => '2018-09-30',
'teacher_id' => '1',
'teacher_name' => '',
'student_id' => '14',
'student_name' => '',
'form_done_id' => '14',
],
[
'id' => '3',
'one_day' => '2018-09-30',
'teacher_id' => '1',
'teacher_name' => '',
'student_id' => '15',
'student_name' => '',
'form_done_id' => '17',
]
];
$arr = $newarr = [];
foreach ($data as $key= > $val) {
$pre_id = $val['id'];
$arr[$key] = array_splice($val,4);
$arr[$key]['pre_id'] = $pre_id;
$newarr[$key] = array_splice($val,0,4);
}
$info = a_array_unique ($newarr);
$newdata = $cc = [];
var_dump ($info);
var_dump ($arr);
foreach ($arr as $k = > $v) {
)
foreach($info as $kk=>$vv){
$newdata[$kk] = $vv;
if($v['pre_id'] == $vv['id']){
$cc[$v['pre_id']]['list'][] = $v;
}
}
}
$zhongji = [];
foreach ($newdata as $aa= > $bb) {
foreach($cc as $dd=>$ee){
if($bb['id'] == $dd){
$zhongji[] = array_merge($bb,$ee);
}
}
}
function a_array_unique ($array) {
$out = array();
foreach ($array as $key=>$value) {
if (!in_array($value, $out)){
$out[$key] = $value;
}
}
$out = array_values($out);
return $out;
}
Let's do something stupid