我有一个关于从包含对象的数组中删除项的问题,相同的代码在其他应用程序中运行良好。在循环数组 $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;
}
}