gpt4 book ai didi

php - 父子关系树所需的递归函数?

转载 作者:行者123 更新时间:2023-11-30 22:33:39 31 4
gpt4 key购买 nike

Scenario:

我的类(class)有类次(早上、晚上等)[类(class)是类次的父级]。
并且对于每个类次都有部分(A Green,A blue,B ect)[shift 是部分的父级]。

graph:

enter image description here

我通过连接获得的当前记录:

    $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 result:

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

任何人都可以帮忙。

最佳答案

我无法从您发布的信息中推断出正确设置您的 shift 键所需的信息,但是您所要做的就是修改 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com