这个目的是创建一个搜索条件,最终处理输出以显示基于匹配增加顺序的结果
“如果和foreach条件[关闭]中的PHP内爆函数”是一个不同的问题!
为什么以下代码失败?
$terms = array("HELLO", "HI", "HOWDY");
$row = array("HELLO", "HI", "Hey");
$chkcond = "in_array('".implode("',$"."row".")"." && in_array"."('",$terms)."',$"."row)";
echo "$chkcond<br/><br/>";
if ($chkcond) {
echo "All Found in Array !<br>";}else{echo "Not Found !<br/>";
}
回声结果是
in_array('HELLO',$row) && in_array('HI',$row) && in_array('HOWDY',$row)
if条件输出"All Found in Array !"
当if
条件表明所有三个字词都必须在行数组中为“All Found in Array
”时,为什么在“True
”时它会返回Howdy
“行数组中不存在?
答案 0 :(得分:4)
juste使用array_diff
http://php.net/array_diff
<?php
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey");
$diff = array_diff($terms, $row);
if (0 === count($diff)){echo "All terms Found in Array row !<br>";}else{echo "Not all terms Found in Array row !<br>";}
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey","HOWDY");
$diff = array_diff($terms, $row);
if (0 === count($diff)){echo "All terms Found in Array row !<br>";}else{echo "Not all terms Found in Array row !<br>";}
答案 1 :(得分:1)
您可以使用array_diff
类似的东西:
<?php
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey");
$chkcond=array_diff($terms, $row);
var_dump($chkcond);
if(empty($chkcond)) {
echo "All Found in Array !<br>";
} else {
echo "Not Found !<br>";
}
?>
答案 2 :(得分:1)
不确定你在尝试什么,但你可以使用array_diff。
admin_passwords
答案 3 :(得分:1)
如果您想坚持使用in_array
,则需要循环使用条款:
<?php
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey");
$chkcond = true;
foreach($row as $needle){
if(!in_array($needle, $terms)){
$chkcond = false;
break;
}
}
if($chkcond){
echo "All Found in Array !<br>";
} else {
echo "Not Found !<br>";
}
?>
哎呀重新阅读你甚至不需要循环的手册http://php.net/manual/en/function.in-array.php:
<?php
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey");
if(in_array($row, $terms)){
echo "All Found in Array !<br>";
} else {
echo "Not Found !<br>";
}
?>