我在使用对象从数组中删除项目时遇到问题,在其他应用程序中运行相同的代码。循环后,数组$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;
}
}
答案 0 :(得分:0)
为了删除/设置null数组元素,我们需要传递数组unset($children[$key]);
的特定索引 -
foreach ($children as $key=>$child) {
$removed = $this->MD->remove($child->id, $this->categories_table);
if ($removed) {
//problem here
unset($children[$key]);
}
}
希望这会对你有所帮助。