-4

My issue is I have two numbers in the array 5,12 my issue is the code below is returning "1" as true because of the "1" in number 12- it sees the number 1 in 12. How do I tell it to read the ENTIRE number.

<?php 
$pos = strpos($foo,"1");

if($pos === false) {
    // do this if its false
    echo "<img src='../PICS/no.png' width='20' height='20' />"; 
}
else {
    echo "<img src='../PICS/yes.png' width='20' height='20' />"; 
} 
?>
4

2 回答 2

4

这是您正在使用的函数的签名:

int strpos (字符串$haystack , 混合 $needle [, int $offset = 0 ] )

如果你用一个数组作为第一个参数来喂它......

<?php
$foo = array(5,12);
$pos = strpos($foo,"1");

...数组将被转换为字符串,您会收到通知...

警告:strpos() 期望参数 1 是字符串,给定数组

结果字符串将包含“Array”,字面意思是:

var_dump( @(string)array(5,12) ); // string(5) "Array"

而且您的搜索1将始终失败,因为Array不包含它。

于 2012-12-05T17:46:06.063 回答
3

利用in_array()

if (in_array(1,$myarray)){
    // found in the array
}
于 2012-12-05T17:38:54.517 回答