2016-08-15 7 views
0

아래 PHP 코드는 Google Merchant Center에서 Woocommerce 카테고리를 제외합니다. 코드를 더 짧게 만들려면 in_array을 어떻게 조합 하시겠습니까? 하나의 값이 true를 반환하기에 충분한 경우PHP에서 다중 배열 결합

// Exclude categories from my Google Product Feed 

function lw_gpf_exclude_product($excluded, $product_id, $feed_format) { 
    // Return TRUE to exclude a product, FALSE to include it, $excluded to use the default behaviour. 
    $cats = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids')); 
    if (in_array(60, $cats)) { 
     return TRUE; 
    } 
    if (in_array(63, $cats)) { 
     return TRUE; 
    } 
    if (in_array(88, $cats)) { 
     return TRUE; 
    } 
    if (in_array(89, $cats)) { 
     return TRUE; 
    } 
    return $excluded; 
} 
add_filter('woocommerce_gpf_exclude_product', 'lw_gpf_exclude_product', 11, 3); 
+0

[http://php.net/manual/en/function.array-intersect.php](http://php. net/manual/ko/function.array-intersect.php) – cske

답변

2

, 당신은 두 배열을 교차 할 수 있으며 결과 배열 크기 (요소가)이있는 경우, 적어도 하나의 값은 두 배열에 존재한다.

return count(array_intersect([60, 63, 88, 89], $cats)) > 0; 
0

array_intersect 기능을 사용하여 솔루션 :

... 
if (array_intersect([60, 63, 88, 89], $cats)){ 
    return TRUE; 
} 
0

이 시도. 코드 짧게하는 기능

in_array_any
이런

return !empty(array_intersect(array(60,63,88,89),$cats)); 
0

사용

 

    function in_array_any($needles, $haystack) { 
     return !!array_intersect($needles, $haystack); 
    } 

    if(in_array_any([60,63,88,89], $cats)){ 
     return TRUE; 
    }