使用PHP,如何在数组中搜索部分字符串,然后显示数组的多个部分?

时间:2015-01-27 17:08:59

标签: php arrays search multidimensional-array substring

我发现我不认为我想搜索数组键并打印它们的值,我希望搜索一部分填写的条目,即,如果我搜索了“haw'它将在第一列中返回Hawthorn,Hawt等每一行。

这就是我目前用于搜索的内容,如果我为这些值提供全名,它可以正常工作(虽然我意识到它可能很丑陋且效率低下)。但是,我希望能够搜索“geoff'找到所有的geoffrey,然后显示他们行的相关部分。

我已经尝试了各种方法将字符串转换为数组,将数组转换为字符串,这是基于我从我试过的各种函数中得到的错误,但我无法解决它。它在搜索中是如此常见的功能我真的认为我错过了一些重要的东西。我很新,我需要帮助!

最近我尝试使用' In_array'但这也没有帮助。谢谢,如果有人可以排序它!

  

    $sitename = $_GET['sitename'];
    $upsitename = strtolower($sitename);

    $area = $_GET['area'];
    $uparea = strtolower($area);

    $postcode = $_GET['postcode'];
    $uppostcode = strtolower($postcode);

    $file = fopen("houselist.csv","r");
    ini_set('auto_detect_line_endings',TRUE);

    while ( ! feof ( $file ) ) {
        $show = fgetcsv($file);

    if(($upretailer != NULL)and( $upsitename == NULL)and($uparea == NULL)) {        
            if($show[0] == $upretailer) {
                echo "<table class='output'>
    <tr><td>$show[0] </td><td>$show[1]</td><td>$show[2]</td><td>$show[5]</td><td>$show[6]</td><td>$show[8]</td>
                            </table>";
                    }
                }

        if(($upretailer != NULL) and ($upsitename != NULL)and($uparea == NULL)) {
            if(($show[0] == $upretailer) and ($show[2] == $upsitename)) {
                echo "<table class='output'>
                                <tr><td>$show[0] </td><td>$show[1]</td><td>$show[2]</td><td>$show[5]</td><td>$show[6]</td><td>$show[8]</td>
                            </table>";
                                }
                            }

        if(($upretailer != NULL) and ($upsitename != NULL)and($uparea != NULL)) {
            if(($show[0] == $upretailer) and ($show[2] == $upsitename) and ($show[5] == $uparea)) {
                echo "<table class='output'>
                                <tr><td>$show[0] </td><td>$show[1]</td><td>$show[2]</td><td>$show[5]</td><td>$show[6]</td><td>$show[8]</td>
                            </table>";
                                }
                            }

        if(($upretailer == NULL) and ($upsitename != NULL)and($uparea != NULL)) {
            if(($show[2] == $upsitename) and ($show[5] == $uparea)) {
                echo "<table class='output'>
                                <tr><td>$show[0] </td><td>$show[1]</td><td>$show[2]</td><td>$show[5]</td><td>$show[6]</td><td>$show[8]</td>
                            </table>";
                                }
                            }

        if(($upretailer == NULL) and ($upsitename == NULL)and($uparea != NULL)) {
            if($show[5] == $uparea) {
                echo "<table class='output'>
                                <tr><td>$show[0] </td><td>$show[1]</td><td>$show[2]</td><td>$show[5]</td><td>$show[6]</td><td>$show[8]</td>
                            </table>";
                                }
                            }

        if(($upretailer == NULL) and ($upsitename != NULL)and($uparea == NULL)) {
            if($show[2] == $upsitename) {
                echo "<table class='output'>
                                <tr><td>$show[0] </td><td>$show[1]</td><td>$show[2]</td><td>$show[5]</td><td>$show[6]</td><td>$show[8]</td>
                            </table>";
                                }
                            }
        if(($upretailer != NULL) and ($upsitename == NULL)and($uparea != NULL)) {
            if(($show[0] == $u)and($show[5] == $uparea)){
                echo "<table class='output'>
                                <tr><td>$show[0] </td><td>$show[1]</td><td>$show[2]</td><td>$show[5]</td><td>$show[6]</td><td>$show[8]</td>
                            </table>";
                                }
                            }
    }
        fclose($file);
?> 

2 个答案:

答案 0 :(得分:0)

在脚本开头的某处定义搜索/比较功能

function searchString($haystack, $needle) {
    return strpos($haystack, $needle) === 0 ? true : false;
}

并在IF语句f.e。

中使用它
if(searchString($show[0], $upretailer) && searchString($show[2], $upsitename)) {
    ...
}

答案 1 :(得分:0)

<?php

$sitename = $_GET['sitename'];
$upsitename = strtolower($sitename);

$area = $_GET['area'];
$uparea = strtolower($area);

$postcode = $_GET['postcode'];
$uppostcode = strtolower($postcode);

$file = fopen("houselist.csv","r");
ini_set('auto_detect_line_endings',TRUE);

$i=0;

while ( ! feof ( $file ) ) {
$show = fgetcsv($file);

  $track="nope";

  foreach ($show as $val) {

   if (strpos($val,$upsitename) !== false)
   {
    $track="true";
    echo $upsitename . " appeared in " . $val . " on line " . $i . "</br>";
   }
   if (strpos($val,$uppostcode) !== false)
   {
    $track="true";
    echo $uppostcode . " appeared in " . $val . " on line " . $i . "</br>";
   }
   if (strpos($val,$uparea) !== false) 
   {
    $track="true";
    echo $uparea . " appeared in " . $val . " on line " . $i . "</br>";
   }

  }

  if($track=="true")
  {
  echo 
  "</br></br><table class='output'>
  <tr>
  <td>$show[0] </td>
  <td>$show[1]</td>
  <td>$show[2]</td>
  <td>$show[5]</td>
  <td>$show[6]</td>
  <td>$show[8]</td>
  </tr>
  </table>";
  }

$i++;      
}

fclose($file);
?>