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

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

问题描述:

I have retrieved data from WordPress database with a loop. The query is as below.

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

Here $product_cookingIDs = $family_list_total[$k]->food_complements; returns four serialized arrays as listed below.

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

Now I have converting these serialized arrays to simple arrays usimg the code given below. And also checking if these are arrays or not.

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

And the above code returns this,

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

Now I have changed the above code to the code given below,

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

And it returns two arrays as listed below,

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

But when i am trying to check in_array for these results with the code given below,

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

It returns Warning: in_array() expects parameter 2 to be array with the same line number in the code.

Try this code

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