【问题标题】:in_array() expects parameter 2 to be array, string given in Classipressin_array() 期望参数 2 是数组,Classipress 中给出的字符串
【发布时间】:2014-06-19 16:48:50
【问题描述】:

我正在为 wordpress 使用Classipress theme,我正在尝试将我的精选广告粘贴在类别中。

我找到了返回此错误的代码:

Warning: in_array() expects parameter 2 to be array, string given in loop-ad_listing.php on line 26

要使用置顶代码,我必须编辑两个页面并插入两个代码,然后我将发布错误部分:

第一:loop-ad_listing.php

代码1:

global $postisfeatured;
global $featurePostArray;

if ($postisfeatured == "1") 
    array_push($featurePostArray, $post->ID);

if (in_array($post, "ID", $featurePostArray) && $postisfeatured != "1") 
    echo '<div class="hide">';

代码2:

if ($postisfeatured != "1") {
    appthemes_after_endwhile();
    $postisfeatured = "";
}

那条线:if (in_array($post,"ID",$featurePostArray) &amp;&amp; $postisfeatured != "1") { 是错误。

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    我想我解决了这个问题。我在表单字段select multiple 中遇到了类似的情况,它自动创建的不是简单变量而是数组类型变量(PHP 弱变量类型)。因此,当我首先在代码中声明该值时:

    1. $lang='';
    2. 然后$lang=$_POST['lang'];
    3. <select id="lang" name="lang[]" multiple size="3"> <option value="en"<?php if(in_array('en',$lang)) echo ' selected';?>>English</option> <option value="fr"<?php if(in_array('fr',$lang)) echo ' selected';?>>French</option> <option value="pl"<?php if(in_array('pl',$lang)) echo ' selected';?>>Polish</option> </select><br>
    4. 然后我尝试运行脚本并没有选择我得到已知错误“in_array() 需要参数 2...”的选项
    5. 解决方案是将(在第 1 点)设置为空数组$lang=array();

    【讨论】:

      【解决方案2】:

      您使用“ID”作为第二个参数,它是一个字符串。请尝试以下操作:

      if ( is_array($featurePostArray) && in_array($post->ID, $featurePostArray) ) {
          //some action
      }
      

      【讨论】:

        【解决方案3】:

        您没有使用应使用的 in_array 函数。

        在第二个参数中你放置了一个字符串(即"ID"),这是错误的;您应该将要搜索的数组放在那个位置。

        结果应该是这样的:

        $valueToSearch = "a";
        $arrayToSearch = array("a", "b", "c");
        echo in_array($valueToSearch, $arrayToSearch);
        

        Please refer to the documentation

        【讨论】:

          【解决方案4】:

          in_array 的签名看起来像:

            in_array($needle, $haystack, $strict = FALSE);
          

          地点:

          needle 是您要搜索的字符串、整数、资源等。

          haystack 是您要搜索的数组

          strict(可选)如果(或不)匹配项应该相同(===

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-03-27
            • 1970-01-01
            • 2017-11-14
            • 1970-01-01
            • 1970-01-01
            • 2019-12-16
            • 1970-01-01
            相关资源
            最近更新 更多