Ask for help: php handles this array

clipboard.png

$data source code is as follows:

$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",
    ]
];

look forward to your advice, thank you!

Php
Jul.23,2021

ideas:

  1. array_walk array
  2. Group according to teacher_id field
  3. 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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b31652-4031b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b31652-4031b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?