2016-12-30 5 views
-3

Мне нужно увидеть, есть ли несколько значений в категории wordpress. Как перейти к проверке нескольких категорий с помощью in_array()?Как использовать функцию PHP in_array()?

if (in_array(13, $cats)) { 
     return TRUE; 
    } 

Мне нужно, чтобы проверить категории: 13, 210, 311, 312

EDIT

Полный код:

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(13, $cats)) { 
     return TRUE; 
    } 
    return $excluded; 
} 

Спасибо за вашу помощь!

+2

http://php.net/manual/en/function.in-array.php – castis

+0

Это будет зависеть от того, что ваш массив выглядит. Опубликуйте то, что вы пробовали, с созданным им результатом. – ivanivan

+0

@ivanivan выглядит так: 'array ('fields' => 'ids')' –

ответ

1

Хотя ваш вопрос действительно ambiguos я полагаю, вы хотите проверить, если 13, 210, 311, 312 являются в $ кошек, то нам этот фрагмент:

<?php 
$array = array(13, 210, 311, 312); 
$cats = array(13, 210, 111111, 5555, 66666, 77777, 311, 312); 
if($array == array_intersect($array, $cats)){ 
    return true; 
} 

UPDATE:

Так что я думаю, что знаю, чего вы хотите, но я не уверен. В любом случае, это обновленная функция.

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')); 
    // https://codex.wordpress.org/Function_Reference/wp_get_post_terms 
    // check to make sure the response in not wp error, see Return Values 
    if(! is_wp_error($cats)){ 
     $check = array(13, 210, 311, 312); 
     $ids = array(); 
     // each array element is an object, see Variables in Returned Object 
     // method 1: more reliable 
     foreach($cats as $cat){ 
      $ids[] = $cat->term_id; 
     } 
     if($check == array_intersect($check, $ids)){ 
      return true; 
     } 
     // method 2: just compares the number of elements found 
     $check = array(13, 210, 311, 312); 
     $count = 0; 
     foreach($cats as $cat){ 
      if(in_array($cat->term_id, $check)){ 
       $count++; 
      } 
     } 
     if($count === count($check)){ 
      return true; 
     } 
    } 
    return $excluded; 
} 
0

Вы можете использовать Еогеасп looop для проверки всех категорий как

$array = array('mykey' => 'myvalue', 'mykey2'=> 'myvalue2', 'mykey3'=> 'myvalueN'); 
foreach($array as $key=> $item){ 
    if (in_array($key, $cats)) { 
     echo "Key or id is available"; 
    } 
} 

Ссылка: http://php.net/manual/en/function.in-array.php

0

Вы можете использовать цикл по каждому элементу

<?php 
$categories = [13, 210, 311, 312]; 
foreach ($categories as $category) { 
if (in_array ($category, $cats) { 
echo "{$category} is in array!"; 
} 
} 
?>