【发布时间】:2014-07-08 13:36:57
【问题描述】:
我有一个嵌套的类别列表,其中 DB 看起来像这样:
id parent_id title
83 81 test3
86 83 test1
87 83 test2
94 87 subtest2.1
95 87 subtest2.2
...etc...
我需要将所有子元素 id 添加到每个父 id 的 $checked_elements 数组中。
因此,如果选择了某个特定的 id,它会自动添加到 $checked_elements 的数组中。如下所示:
我被递归函数困住了,关于如何递归地添加每个父项 id 的子项?我的功能不会比第二级更深,谁能告诉我如何解决它以便检查所有子项?
private function delete( ){
// Collect all checked elements into the array
$checked_elements = $this->input->post('checked');
// Recursive function to check for child elementts
foreach( $checked_elements as $key => $value ){
// Get records where parent_id is equal to $value (checked item's id)
$childs = $this->categories_model->get_by(array('parent_id' => $value));
// Add found record's id into the array
foreach( $childs as $child ){
$checked_elements[] => $child->id;
}
}
}
【问题讨论】:
-
Creating a Multi-Dimentional from another Multi Dimensional Array 的可能重复项......同样的问题其他公式......
-
@bwoebi 感谢您的通知,但我觉得您不明白我想要什么。我希望将所有找到的元素添加到 1 级数组中,如下所示:
$key => $id。重复的文章与我想要的略有不同,也许有相同的逻辑,但这就是我想要理解的,如果你能得到关于我的问题的更多信息,我会非常友好。谢谢
标签: php arrays codeigniter recursion