PHP 改变查询结果的数组格式,效率问题
$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;
}
}
我用的框架是ThinkPHP 其中刚查询出来的值 $courseDetail 的格式如下
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"
}
}
但是这种格式有一个问题 course_id与student_id是一个一对多的关系,这样查询出来的数据就会有course_id和teacher_id相等的几个数据,我现在想把数据按course_id归成一条 就写了上面的循环,$data结果如下:
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"
}
}
}
}
我感觉我写的方法有问题 效率低,代码质量比较差,但又想不出其他方法。想问问大家有没有更好的方法,谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论