我试图创建一个数组,其中包含了数据库中所有的父项和子项的ID。但是我得到的只是一个数据。
我的理想输出是:
array('160', '161', '162', '163', '164');
我得到的只是
array('160');
以下是我目前所做的工作。
public function arrayId(array $elements) {
$where_in = array();
foreach($elements as $element){
if($element->isArray) {
$elems = $this->my_model->select_where('tbl_policies', array('parent_id' => $element->id));
$this->arrayId($elems);
}
$where_in[] = $element->id;
}
return $where_in;
}
$id = 160; //for instance
$elements = $this->my_model->select_where('tbl_policies', array('id' => $id));
$where_in = $this->arrayId($elements);
die(print_r($where_in));
我在这里获取的数据:表策略
对我来说,组织问题有点困难。所以如果有什么不清楚的地方,请在下面评论,我会尽我所能让它更容易理解。提前感谢。
1条答案
按热度按时间u1ehiz5o1#
我明白,你想删除一个父代及其所有子代和孙代。但你这样做不是直接和顺序,而是想收集所有的ID记录要删除。你应该去以下步骤:
1.编写一个递归函数,例如getChildrenIds(parentId)。
1.在这个函数中,你应该迭代子函数,如果一个子函数有“isArray”标志(根据你的应用程序逻辑),那么你应该调用getChildrenIds(currentChildId)
我已经写了下面的函数。它应该工作。
请注意,$where_in通过引用传递给递归函数getChildrenIds()并在那里填充。