dua6992 2016-04-16 11:48
浏览 34
已采纳

警告: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.

  • 写回答

1条回答 默认 最新

  • dongwu8064 2016-04-16 12:18
    关注

    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);                 
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog