【发布时间】:2016-12-14 01:42:42
【问题描述】:
我在使用对象从数组中删除项目时遇到问题,相同的代码在其他应用程序中运行。循环后数组$categories 应该是空的。下面的代码删除所有子类别,如果用户将第二个参数传递为 TRUE,则删除父类别,如果父类别没有子类别,则仅删除它。
//the $id of the category to be removed
// $remove_children is state if you accept removing children categories
function remove_category($id = null, $remove_children = false) {
if ($this->MD->is_category($id) && is_bool($remove_children)) {
//get all children category
$children = $this->get_children_categories($id);
if (!$children) {
return $this->MD->remove($id, $this->categories_table);
} else {
if ($remove_children && is_array($children)) {
unset($children['parent_category']);
foreach ($children as $child) {
$removed = $this->MD->remove($child->id, $this->categories_table);
if ($removed) {
//problem here
unset($child);
}
}
//the $children is not empty after remove all the items
if (empty($children)) {
return $this->MD->remove($id, $this->categories_table);
} else {
return false;
}
} else {
return false;
}
}
} else {
return false;
}
}
【问题讨论】:
标签: php arrays oop codeigniter-3 unset