简体   繁体   English

在if语句中使用爆破时in_array失败

[英]in_array fails when using implode in if statement

This attamept is to create a search condition where eventually the output will be processed to show results based on increasing order of matches 这种限制是为了创建搜索条件,最终将根据匹配的递增顺序来处理输出以显示结果

"PHP implode function in if and foreach condition [closed]" is a different question ! “在if和foreach条件下PHP内爆功能[关闭]”是一个不同的问题!

Why is the following code failing ? 为什么以下代码失败?

$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/>";
}

The echo result is 回声结果是

in_array('HELLO',$row) && in_array('HI',$row) && in_array('HOWDY',$row)

And the if condition outputs "All Found in Array !" 如果条件条件输出"All Found in Array !"

When the if condition says that all three terms have to be in the row array to be " All Found in Array ", then why is it returning True when " Howdy " doesn't exist in the row array ? if条件说所有三个项必须在行数组中才能是“ All Found in Array ”时,为什么当行数组中不存在“ Howdy ”时返回True

juste use array_diff http://php.net/array_diff 只需使用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>";}

You could use array_diff 您可以使用array_diff

Something like: 就像是:

<?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>";
}
?>

Not sure what you are attempting but you can use array_diff. 不确定您要尝试什么,但是可以使用array_diff。

$terms = array("HELLO","HI","HOWDY");
$row   = array("HELLO","HI","Hey");

$differences = array_diff($terms, $row);

if ($differences) {
  echo "All Found in array";
} else {
  "Not Found !<br>";
}

https://secure.php.net/manual/en/function.array-diff.php https://secure.php.net/manual/zh/function.array-diff.php

If you would like to stick with in_array you need to loop your terms: 如果您想坚持使用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>";
    }
?>

Heck re-reading the manual http://php.net/manual/en/function.in-array.php you do not even need to loop: 重读手册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>";
    }

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM