如何使用PHP in_array()函数?

时间:2016-12-30 16:33:29

标签: php wordpress woocommerce

我需要查看wordpress类别中是否有多个值。如何使用in_array()检查多个类别?

0            UE-triggered ERAB Setup Attempts
1           UE-triggered ERAB Setup Successes
4           MME-initiated ERAB Setup Attempts
5          MME-initiated ERAB Setup Successes
8      eNodeB-initiated ERAB Release Attempts
9                 eNodeB-initiated ERAB Drops
11        MME-initiated ERAB Release Attempts
12                   MME-initiated ERAB Drops
14                 ERAB Modification Attempts
15                ERAB Modification Successes
18                    HO Preparation Attempts
19                   HO Preparation Successes
22            HO Resource Allocation Attempts
23           HO Resource Allocation Successes
26                          Handover Attempts
27                         Handover Successes
33                        EPS Attach Attempts
34                       EPS Attach Successes
37                        EPS Detach Attempts
38                       EPS Detach Successes
40                EPS Authentication Attempts
41               EPS Authentication Successes
43                EPS Security Setup Attempts
44               EPS Security Setup Successes
46                 EMM Identification Attepmt
47               EMM Identification Successes
49              EPS Service Request Attemptss
50              EPS Service Request Successes
52              Tracking Area Update Attempts
53             Tracking Area Update Successes

117       S6a Delete Subscriber Data Attempts
118      S6a Delete Subscriber Data Successes
120                 S6a Notification Attempts
121                S6a Notification Successes
126               S11 Create Session Attempts
127              S11 Create Session Successes
130                S11 Create Bearer Attempts
131               S11 Create Bearer Successes
134                S11 Update Bearer Attempts
135               S11 Update Bearer Successes
138             Modify Access Bearer Attempts
139            Modify Access Bearer Successes
141            Release Access Bearer Attempts
142           Release Access Bearer Successes
144       Downlink Data Notification Attempts
145      Downlink Data Notification Successes
147               S11 Delete Session Attempts
148              S11 Delete Session Successes
150                S11 Delete Bearer Attempts
151               S11 Delete Bearer Successes
154                          Suspend Attempts
155                         Suspend Successes
157                           Resume Attempts
158                          Resume Successes
162                ME Identity Check Attempts
163               ME Identity Check Successes
168           Credit Control Initial Attempts
169          Credit Control Initial Successes
171       Credit Control Termination Attempts
172      Credit Control Termination Successes
Name: KPI name, dtype: object

我需要检查类别:13,210,311,312

修改

完整代码:

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

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

虽然你的问题真的很模糊我想你要检查13,210,311,312是否在$ cats中,然后我们这个片段:

<?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;
}

<强>更新

所以我想我知道你想要什么,但我不确定。无论如何这里是更新的功能。

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;
}

答案 1 :(得分:0)

您可以使用foreach 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

答案 2 :(得分:0)

你可以使用foreach循环

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