警告:in_array()要求参数2是数组,即使我已经检查了参数2是一个数组


Warning: in_array() expects parameter 2 to be array, even when i have checked that the parameter 2 is an array

我用一个循环从WordPress数据库中检索数据。查询如下所示。

$family_tot = $wpdb->get_results( "SELECT * FROM `wp_family` WHERE `id` = '".$family_list_total[$k] -> id."%' ORDER BY `wp_family`.`family_name` ASC LIMIT $start_from, $num_rec_per_page", OBJECT );

此处为$product_cookingIDs = $family_list_total[$k]->food_complements;返回下面列出的四个序列化数组。

N;
a:2:{i:0;s:2:"78";i:1;s:2:"80";}
N;
a:2:{i:0;s:2:"79";i:1;s:2:"80";}

现在,我已经使用下面给出的代码将这些序列化的数组转换为简单的数组。还要检查这些是否是数组。

$product_cookingIDs_total = unserialize($product_cookingIDs);
if(is_array($product_cookingIDs_total)){
    print_r($product_cookingIDs_total);
    echo " Is an array <br>";
}
else{
    echo $product_cookingIDs;
    echo " Is not an array <br>";
}

上面的代码返回这个,

N; Is not an array
Array ( [0] => 78 [1] => 80 ) Is an array
N; Is not an array
Array ( [0] => 79 [1] => 80 ) Is an array

现在我已经将上面的代码更改为下面给出的代码,

if($product_cookingIDs != "N;"){
    $product_cookingIDs_total = unserialize($product_cookingIDs);
    print_r($product_cookingIDs_total);
}

它返回下面列出的两个数组,

Array ( [0] => 78 [1] => 80 )
Array ( [0] => 79 [1] => 80 ) 

但是,当我试图用下面给出的代码检查in_array中的这些结果时,

if(in_array($familydata_id, $product_cookingIDs_total) ){
    print_r($product_cookingIDs_total);
    echo "yes";         
}

它返回警告:in_array()期望参数2是代码中具有相同行号的数组

试试这个代码

if($product_cookingIDs != "N;"){
    $product_cookingIDs_total = unserialize($product_cookingIDs);
    if(in_array($familydata_id,$product_cookingIDs_total) ){
        print_r($product_cookingIDs_total);
        echo "yes";
        $cooking = $wpdb -> get_row("SELECT * FROM `wp_woocommerce_custom_field` WHERE id=".$familydata_id);
        $product_list_cooking[] = array($product_list[$i] -> ID,$product->get_image('full'),$product -> get_title(),$cooking -> name,$color_class);                 
        }
    }