在in_array检查后返回数组结果?

Pau*_*nak 1 php arrays

我试图读取一个文件,其中包含++++作为分隔符的HTML列表项.基本上,我试图在数组中搜索"nmb"(存储在类中)的文本中的每个项目,然后显示数组中具有"nmb"的所有项目.但是,我当前的代码不起作用.它显示......没有.并且没有错误.我在这里错过了什么?

<?php
$term = "nmb";
$f_contents = file_get_contents("../includes/inc-condo-list.php"); //get the entire file
$array = explode("++++",$f_contents); //explode (create an array) seperated by new line elements
$datotal = count($array);  //gives me a TOTAL count.. maybe for later use


foreach ($array as $str ){    //go through each item in array
    if(!in_array($term, $array)) {
    echo $str;
    }
}

unset ($array);

?>
Run Code Online (Sandbox Code Playgroud)

以下是从读取文件引入的列表项的示例.

  ++++
    <li class="condo mb"> <a class="condos" href="blah.html"> <img src="someimg.jpg" alt="Narf" /></a> <br />
     <a class="condos" href="somewhere.html">Some Link</a><br />
        4 &amp; 6 Bedrooms<br />

</li>
    ++++
  <li class="condo nmb"> <a class="condos" href="blah.html"> <img src="someimg.jpg" alt="Narf" /></a> <br />
     <a class="condos" href="somewhere.html">Some Link</a><br />
        4 &amp; 6 Bedrooms<br />

</li>
    ++++
Run Code Online (Sandbox Code Playgroud)

任何帮助都将超过赞赏!谢谢.

Joe*_*Joe 5

in_array寻找精确的值匹配.请改用这样的东西.

foreach ($array as $str ){    //go through each item in array
    if(strpos($str, $term) === false) { // if it's not present
        echo $str;
    }
}
Run Code Online (Sandbox Code Playgroud)