简体   繁体   English

in_array检查后返回数组结果?

[英]returning array results after in_array check?

I am trying to read in a file that has HTML list items with ++++ as delimeters. 我正在尝试读取包含HTML列表项(以++++为分隔符)的文件。 Basically, I am trying to search through each item in the array for the text of "nmb" (which is stored in the class) and then display all of the items IN the array that have "nmb". 基本上,我试图在数组的每个项目中搜索“ nmb”的文本(存储在类中),然后在数组中显示所有具有“ nmb”的项目。 However, my current code does not work. 但是,我当前的代码不起作用。 It displays... NOTHING. 它显示...无。 And gives no errors. 并没有错误。 What am I missing here? 我在这里想念什么?

<?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);

?>

Here is an example of the list items being brought in from reading the file. 这是读取文件时引入的列表项的示例。

  ++++
    <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>
    ++++

Any help would be MORE than appreciated! 任何帮助将不胜感激! Thank you. 谢谢。

in_array looks for an exact value match. in_array寻找精确的值匹配。 Use something like this instead. 使用类似这样的东西。

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

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

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