2016-08-08 5 views
2

객체가있는 배열에서 항목을 제거하는 데 문제가 있습니다. 다른 응용 프로그램에서 같은 코드가 실행됩니다. 루프 후 루프 $categories은 비어 있어야합니다. 코드 벨로우즈는 모든 자식 범주를 제거한 다음 사용자가 두 번째 매개 변수를 TRUE로 전달하면 부모 범주를 제거하고 부모에게는 자식이 없으면 제거합니다.객체 배열 내부에서 객체 세트를 해제합니다.

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

이 당신을 도울 것입니다 희망 -/제거 우리가 배열 unset($children[$key]);의 특정 인덱스를 전달해야 널 배열 요소의 설정 방법

//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

unset 함수는 object, variables 및 array와 함께 작동합니다. 그것은 인덱스에 전혀 의존하지 않습니다. 내가 코드를 테스트했는지 확인하고 예상대로 오류가 끝났습니다. 시도해 줘서 고마워. http://php.net/manual/en/function.unset.php –

+0

오, 미안합니다 실수로 당신은 unset ($ children [$ key]); 쓸 필요가 있습니다. –

+0

특정 배열 요소를 설정 해제하기위한 배열의 경우 설정을 해제 할 특정 값 인덱스를 전달해야합니다. –

관련 문제