2013-12-20 2 views
0

주제 배열에 대한 검색 필터를 생성 중입니다. 기본적으로 두 배열을 비교하려고합니다.두 PHP 배열에 일치하는 요소

$filtered_subjects = ["math", "science", "english"] 

나는 이것을 교사가 가르치는 과목과 비교할 것입니다. 기본적으로 배열 filtered_subjects의 모든 구성원이 교사의 배열에 존재하고 해당 교사의 배열에 다른 주제가없는 경우 플래그가 지정되고 제거되어야합니다. 다음 교사를위한

그래서 :

$bob_subjects = ["math"] 
$sam_subjects = ["math", "science", "english", "astronomy"] 
밥을 제거해야

이 샘이 유지되어야 .

PHP로 어떻게하면이 일을 가장 잘 수행 할 수 있을지에 대한 아이디어가 있습니까? 두 배열을 비교하고 교사를 제거해야하는지 여부에 대해 YES 또는 NO 중 하나를 얻으려면 어떻게해야합니까?

$result = array_diff($filtered_subjects, $sam_subjects); 
if in_array($result, $filtered_subjects) 
{ 
    // remove teacher 
} else if in_array($result, $sam_subjects) 
{ 
    // don't remove teacher 
} 

답변

0

사용 array_intersect

$filtered_subjects = ["math", "science", "english"]; 
$sam_subjects = ["math", "science", "english", "astronomy"] 
if (sizeof(array_intersect($filtered_subjects, $sam_subjects)) == sizeof($filtered_subjects)) { 
    // it's your teacher 
} else { 
    // else 
} 
:

방법 중 하나는 나는 다음과 같은 일을하는 것입니다 고려