父子关系树是否需要递归函数

父子关系树是否需要递归函数

场景:


我的课程有班次(上午,晚上等)。
并且每个班次都有部分(A绿色,蓝色,B等)[班次是部分的父项]。


  图形:


php - 父子关系树是否需要递归函数?-LMLPHP

我通过加入获得的当前记录:

    $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
                            )
    )
)


任何可以帮助的。

最佳答案

我无法从发布的信息中推断出所需信息以正确设置您的班次键,但是您要做的就是修改switch语句以正确设置此键:

$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);
$query = $this->db->get();

$results = array();
foreach ($query->result_array() as $row) {
    switch ($row['shift_id']) {
        case 5:
            $shift = 'morning';
        break;

        case 6:
            $shift = 'noon';
        break;
    }

    $results['course_id '.$row['course_id']]['shift_'.$shift]['section_id'] = $row['section_id'];
}

关于php - 父子关系树是否需要递归函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33164612/

10-10 18:10