php - unset object inside an array of objects -


i have problem remove items array object, same code run in other app. after looping array $categories should empty. code bellow removes children categories removes parent category if user pass second parameter true, if parent doesn't have child remove only.

//the $id of category removed  // $remove_children state if accept removing children categories   function remove_category($id = null, $remove_children = false) {     if ($this->md->is_category($id) && is_bool($remove_children)) {         //get 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 $child) {                     $removed = $this->md->remove($child->id, $this->categories_table);                     if ($removed) {                         //problem here                         unset($child);                     }                 }                 //the $children not empty after remove items                 if (empty($children)) {                     return $this->md->remove($id, $this->categories_table);                 } else {                     return false;                 }             } else {                 return false;             }         }     } else {         return false;     } } 

for removing/setting null array element need pass particular index of array unset($children[$key]);-

foreach ($children $key=>$child) {                     $removed = $this->md->remove($child->id, $this->categories_table);                     if ($removed) {                         //problem here                         unset($children[$key]);                     }                 } 

hope you.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -