【发布时间】:2015-10-16 07:17:39
【问题描述】:
场景:
我的课程有班次(早上、晚上等)[课程是班次的父级]。
每个班次都有部分(绿色,蓝色,B ect)[班次是部分的父级]。
图表:
我通过连接获得的当前记录:
$this->db->select("sections.section_id,c.course_id,s.id as shift_id");
$this->db->from($this->_table);
$this->db->join('shift s', 's.id = sections.shift_id', 'left');
$this->db->join('courses c', 's.course_id = c.course_id', 'left');
$this->db->join('employees e', 'e.employee_id = sections.head_id', 'left');
$this->db->distinct();
$this->db->order_by('section_id', 'desc');
$this->db->where('c.course_id',$course_id);
查询结果:
Array
(
[0] => Array
(
[course_id] => 3
[section_id] => 5
[shift_id] => 7
)
[1] => Array
(
[course_id] => 2
[section_id] => 4
[shift_id] => 5
)
[2] => Array
(
[course_id] => 2
[section_id] => 3
[shift_id] => 6
)
[3] => Array
(
[course_id] => 1
[section_id] => 2
[shift_id] => 4
)
[4] => Array
(
[course_id] => 1
[section_id] => 1
[shift_id] => 4
)
)
想要这样的结果:
Array
(
[course_id 3] => Array
(
[shift id] => array
(
[section_id] => 5
)
)
[course_id 2] => Array
(
[shift_morning] =>array
(
[section_id] => 4
)
[shift_noon] =>array
(
[section_id] => 3
)
)
[course_id 1] => Array
(
[shift_morning] =>array
(
[section_id] => 2
)
[shift_noon] =>array
(
[section_id] => 1
)
)
)
请大家帮忙。
【问题讨论】:
-
你的问题到底是什么?
-
根据父子关系重新排列结果。我知道 sql 不支持分层数据。
-
那么,您尝试过什么?什么不起作用?
-
使用了哪些表/列?你如何获得第一个数组?在结果之前显示代码。
标签: php mysql join foreach codeigniter-3