PHP changes the array format of query results, which is a problem of efficiency.

$courseDetail = M("course")
            ->field("c.course_id,c.name,from_unixtime(c.time) as course_time,c.room_id,tu.teacher_id,tu.relname as teacher_name, tu.mobile as teacher_mobile,su.student_id,su.nickname as student_name,su.mobile as student_mobile")
            ->alias("c")
            ->join("left join __USER_COURSE__ uc using(course_id)")
            ->join("left join __TEACHER_USER__ tu on c.teacher_id = tu.teacher_id")
            ->join("left join __STUDENT_USER__ su on uc.user_id = su.student_id")
            ->where($cond)
            ->order("time ".$order)
            ->select();
        if (!$courseDetail){
            $data = "";
        }else{
            $data = [];
            $tmp = $courseDetail;
            foreach ($courseDetail as $key => &$value){
                $course_id = $value["course_id"];
                $value["students"] = array(
                    array(
                        "student_id"    => $value["student_id"],
                        "name" => $value["student_name"],
                        "mobile"    => $value["student_mobile"]
                    )
                );
                foreach($tmp as $k => $v){
                    if ($course_id == $v["course_id"] && $v["student_id"] != $value["student_id"]){
                        $arr = array(
                            "student_id"    => $v["student_id"],
                            "name" => $v["student_name"],
                            "mobile"    => $v["student_mobile"]
                        );
                        array_push($value["students"],$arr);
                        unset($courseDetail[$k]);
                    }
                }
                unset($value["student_id"],$value["student_name"],$value["student_mobile"]);
                $data[] = $value;
            }
        }
< H2 > the frame I use is ThinkPHP, where the value $courseDetail I just queried is in the following format < / H2 >
        
array(5) {
  [0]=>
  array(10) {
    ["course_id"]=>
    string(4) "5202"
    ["name"]=>
    string(6) ""
    ["course_time"]=>
    string(19) "2018-03-31 11:00:00"
    ["room_id"]=>
    string(25) "264_2018-03-31 11:00_manu"
    ["teacher_id"]=>
    string(3) "264"
    ["teacher_name"]=>
    string(12) ""
    ["teacher_mobile"]=>
    string(11) "13900139002"
    ["student_id"]=>
    string(3) "272"
    ["student_name"]=>
    string(12) ""
    ["student_mobile"]=>
    string(11) "13800138001"
  }
  [1]=>
  array(10) {
    ["course_id"]=>
    string(4) "5230"
    ["name"]=>
    string(6) ""
    ["course_time"]=>
    string(19) "2018-03-30 23:00:00"
    ["room_id"]=>
    string(25) "290_2018-03-30 23:00_4oBF"
    ["teacher_id"]=>
    string(3) "290"
    ["teacher_name"]=>
    string(15) ""
    ["teacher_mobile"]=>
    string(11) "13900139030"
    ["student_id"]=>
    string(3) "367"
    ["student_name"]=>
    string(12) ""
    ["student_mobile"]=>
    string(11) "13800138050"
  }
  [2]=>
  array(10) {
    ["course_id"]=>
    string(4) "5230"
    ["name"]=>
    string(6) ""
    ["course_time"]=>
    string(19) "2018-03-30 23:00:00"
    ["room_id"]=>
    string(25) "290_2018-03-30 23:00_4oBF"
    ["teacher_id"]=>
    string(3) "290"
    ["teacher_name"]=>
    string(15) ""
    ["teacher_mobile"]=>
    string(11) "13900139030"
    ["student_id"]=>
    string(3) "276"
    ["student_name"]=>
    string(12) ""
    ["student_mobile"]=>
    string(11) "13800138003"
  }
  [3]=>
  array(10) {
    ["course_id"]=>
    string(4) "5229"
    ["name"]=>
    string(6) ""
    ["course_time"]=>
    string(19) "2018-03-30 22:30:00"
    ["room_id"]=>
    string(25) "290_2018-03-30 22:30_TQ8o"
    ["teacher_id"]=>
    string(3) "290"
    ["teacher_name"]=>
    string(15) ""
    ["teacher_mobile"]=>
    string(11) "13900139030"
    ["student_id"]=>
    string(3) "367"
    ["student_name"]=>
    string(12) ""
    ["student_mobile"]=>
    string(11) "13800138050"
  }
  [4]=>
  array(10) {
    ["course_id"]=>
    string(4) "5228"
    ["name"]=>
    string(6) ""
    ["course_time"]=>
    string(19) "2018-03-30 22:00:00"
    ["room_id"]=>
    string(25) "290_2018-03-30 22:00_4PBm"
    ["teacher_id"]=>
    string(3) "290"
    ["teacher_name"]=>
    string(15) ""
    ["teacher_mobile"]=>
    string(11) "13900139030"
    ["student_id"]=>
    string(3) "367"
    ["student_name"]=>
    string(12) ""
    ["student_mobile"]=>
    string(11) "13800138050"
  }
}
< H2 > but there is a problem with this format: course_id and student_id have an one-to-many relationship, so the queried data will have several data equal to course_id and teacher_id. Now I want to course_id the data into one line and write the above loop. The result of $data is as follows: < / H2 >
array(4) {
  [0]=>
  array(8) {
    ["course_id"]=>
    string(4) "5202"
    ["name"]=>
    string(6) ""
    ["course_time"]=>
    string(19) "2018-03-31 11:00:00"
    ["room_id"]=>
    string(25) "264_2018-03-31 11:00_manu"
    ["teacher_id"]=>
    string(3) "264"
    ["teacher_name"]=>
    string(12) ""
    ["teacher_mobile"]=>
    string(11) "13900139002"
    ["students"]=>
    array(1) {
      [0]=>
      array(3) {
        ["student_id"]=>
        string(3) "272"
        ["name"]=>
        string(12) ""
        ["mobile"]=>
        string(11) "13800138001"
      }
    }
  }
  [1]=>
  array(8) {
    ["course_id"]=>
    string(4) "5230"
    ["name"]=>
    string(6) ""
    ["course_time"]=>
    string(19) "2018-03-30 23:00:00"
    ["room_id"]=>
    string(25) "290_2018-03-30 23:00_4oBF"
    ["teacher_id"]=>
    string(3) "290"
    ["teacher_name"]=>
    string(15) ""
    ["teacher_mobile"]=>
    string(11) "13900139030"
    ["students"]=>
    array(2) {
      [0]=>
      array(3) {
        ["student_id"]=>
        string(3) "367"
        ["name"]=>
        string(12) ""
        ["mobile"]=>
        string(11) "13800138050"
      }
      [1]=>
      array(3) {
        ["student_id"]=>
        string(3) "276"
        ["name"]=>
        string(12) ""
        ["mobile"]=>
        string(11) "13800138003"
      }
    }
  }
  [2]=>
  array(8) {
    ["course_id"]=>
    string(4) "5229"
    ["name"]=>
    string(6) ""
    ["course_time"]=>
    string(19) "2018-03-30 22:30:00"
    ["room_id"]=>
    string(25) "290_2018-03-30 22:30_TQ8o"
    ["teacher_id"]=>
    string(3) "290"
    ["teacher_name"]=>
    string(15) ""
    ["teacher_mobile"]=>
    string(11) "13900139030"
    ["students"]=>
    array(1) {
      [0]=>
      array(3) {
        ["student_id"]=>
        string(3) "367"
        ["name"]=>
        string(12) ""
        ["mobile"]=>
        string(11) "13800138050"
      }
    }
  }
  [3]=>
  array(8) {
    ["course_id"]=>
    string(4) "5228"
    ["name"]=>
    string(6) ""
    ["course_time"]=>
    string(19) "2018-03-30 22:00:00"
    ["room_id"]=>
    string(25) "290_2018-03-30 22:00_4PBm"
    ["teacher_id"]=>
    string(3) "290"
    ["teacher_name"]=>
    string(15) ""
    ["teacher_mobile"]=>
    string(11) "13900139030"
    ["students"]=>
    array(1) {
      [0]=>
      array(3) {
        ["student_id"]=>
        string(3) "367"
        ["name"]=>
        string(12) ""
        ["mobile"]=>
        string(11) "13800138050"
      }
    }
  }
}

I feel that the method I wrote is inefficient and the code quality is poor, but I can"t think of any other way. Would like to ask you if there is a better way, thank you!

Feb.28,2021
Menu