php in_array() 期望参数 2 是数组,Classipress 中给出的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23437671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
in_array() expects parameter 2 to be array, string given in Classipress
提问by user3597087
I'm using Classipress theme
for wordpress, and I'm trying to sticky my featured ads on the on in categories .
我正在使用Classipress theme
wordpress,并且我正在尝试将我的特色广告粘贴在类别中。
I found a code that is return this error:
我发现了一个返回此错误的代码:
Warning: in_array() expects parameter 2 to be array, string given in loop-ad_listing.php on line 26
To use the sticky on top code, I had to edit two pages and insert two codes and I'll post the error part:
要使用置顶代码,我必须编辑两个页面并插入两个代码,然后我将发布错误部分:
First: loop-ad_listing.php
第一的: loop-ad_listing.php
Code1:
代码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">';
Code2:
代码2:
if ($postisfeatured != "1") {
appthemes_after_endwhile();
$postisfeatured = "";
}
That line: if (in_array($post,"ID",$featurePostArray) && $postisfeatured != "1") {
is the error.
那一行:if (in_array($post,"ID",$featurePostArray) && $postisfeatured != "1") {
是错误。
回答by andrew
the signature for in_array
looks like:
的签名in_array
看起来像:
in_array($needle, $haystack, $strict = FALSE);
where:
在哪里:
needle
is the string,int,resource etc which you're searching for.
needle
是您要搜索的字符串、整数、资源等。
haystack
is the array in which you're searching
haystack
是您要搜索的数组
strict
(optional) If (or not) the matched item should be identical (===
)
strict
(可选)如果(或不)匹配的项目应该是相同的 ( ===
)
回答by Intesar Haider
You aren't using the in_array
function as it should be used.
您没有使用in_array
应该使用的功能。
In the second parameter you've placed a string (i.e "ID"
), it's wrong; you should place the array that you wish to search in, at that spot.
在第二个参数中,您放置了一个字符串(即"ID"
),这是错误的;您应该将要搜索的数组放在那个位置。
The outcome should be something like this:
结果应该是这样的:
$valueToSearch = "a";
$arrayToSearch = array("a", "b", "c");
echo in_array($valueToSearch, $arrayToSearch);
回答by sumon cse-sust
You used "ID" as a second parameter which is a string. Try the following:
您使用“ID”作为第二个参数,它是一个字符串。请尝试以下操作:
if ( is_array($featurePostArray) && in_array($post->ID, $featurePostArray) ) {
//some action
}
回答by zoladp
I think I solved this problem. I've got similiar situation in my form field select multiple
which automatically creates not simple variable but array type variable(PHP weak var types). So when I declared the value at first in my code:
我想我解决了这个问题。我的表单字段中有类似的情况,select multiple
它自动创建的不是简单变量而是数组类型变量(PHP 弱变量类型)。因此,当我首先在代码中声明该值时:
$lang='';
- then
$lang=$_POST['lang'];
<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>
- and then I tried to run script and select none of the option I got known error "in_array() expects parameter 2..."
- solution isto set(in point 1.) to empty array
$lang=array();
$lang='';
- 然后
$lang=$_POST['lang'];
<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>
- 然后我尝试运行脚本并没有选择任何选项我得到了已知错误“in_array() 期望参数 2...”
- 解决方案是将(在第 1 点中)设置为空数组
$lang=array();