警告:in_array()期望参数2是数组,即使我已经检查过参数2是一个数组

时间:2016-04-16 11:48:43

标签: php arrays wordpress

  

我已经通过循环从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为数组,代码中的行号相同。

1 个答案:

答案 0 :(得分:0)

  

试试此代码

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