我想检查数组和字符串之间的数据是否匹配。
我试图检查字符串是否相等,但问题是条件返回false,$subex2[1]
和$subdat
的值都是IT100
。我认为问题是你不能将数组与字符串进行比较。有人可以帮我解决这个问题吗?
这里是代码
$subex = $objPHPExcel->getActiveSheet()->getCell('D8')->getValue();
$subex2 = explode(":", $subex);
$q = "select * from class where id='$id'";
$r = mysql_query($q);
$data = mysql_fetch_array($r);
$subdat = $data['subject'];
if($subdat == $subex2[1]) {
echo "Data matched";
}else {
echo "Data doesn't matched";
}
答案 0 :(得分:1)
如评论中所述。一个值前面有一个空格。你可以解决这样的问题:
if(trim($subdat) == trim($subex2[1])) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
对于区分大小写的问题,这个技巧应该适用。
if(strtolower(trim($subdat)) == strtolower(trim($subex2[1]))) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
答案 1 :(得分:0)
此测试工作正常
$subdat = "IT100";
$subex2[1] = "IT100";
if($subdat == $subex2[1]) {
echo "Data matched";
}
您没有比较相同的字符串
答案 2 :(得分:0)
首先使用array_map和trim值,删除空格,然后使用in_array,示例
$subex2 = array_map('trim',$subex2);
if( is_array($subex2) ){
if(in_array($subdata, $subex2)){
echo "Data matched";
} else {
echo "Data doesn't matched";
}
}
使用is_array
参考in_array
https://www.w3schools.com/php/func_array_in_array.asp
答案 3 :(得分:0)
试试这个:
$isMatched = strval($subex2[1]) === strval($subdat) ?: false;