【问题标题】:PHP - Search first names as well as full namePHP - 搜索名字和全名
【发布时间】:2023-03-23 23:31:02
【问题描述】:

目前我的代码搜索数据,但您需要输入全名才能显示结果。我想我需要将我的全名分成名字和姓氏。所以任何一个都可以搜索找到。

我希望它根据全名或名字查找和显示配置文件

-我需要它来显示那个名字的人的结果。但是在我的 csv 文件(存储数据的地方)中,存储了全名,例如“鲍勃·杰拉德”

我猜我也需要在循环中分隔值来搜索名字?我不太确定.. 任何帮助将不胜感激。

这是我所拥有的:

//This gathers the values from the form (search bar) and assigns it to the variable $SearchThis
//isset()
$SearchThis = isset($_POST['Search']) ? $_POST['Search'] : '';
//empty()
$SearchThis = !empty($_POST['Search']) ? $_POST['Search'] : '';



// Grabs the csv file (and its existing data)  and makes it into an array
$csv = array();
$lines = file('data/StaffData.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
    $csv[$key] = str_getcsv($value);
}



//A new array which will display the search results
$new_csv = array(); 

//This displays which rows have matched the search (it is put in an array)

//Looks through full names
$keys = array_keys(array_column($csv, 0), $SearchThis);  //  original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}
//Looks through phone numbers
$keys = array_keys(array_column($csv, 1), $SearchThis);  // original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}
//Looks through gender
$keys = array_keys(array_column($csv, 2), $SearchThis);  // original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}
//Looks through Birthday
$keys = array_keys(array_column($csv, 3), $SearchThis);  // original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}

//Looks through Type of work
$keys = array_keys(array_column($csv, 4), $SearchThis);  // original code
foreach($keys as $index) {                               // Iterate over the keys
    $new_csv[] = $csv[$index];                           // Copy the matching rows to our new array
}

【问题讨论】:

    标签: php search linear-search


    【解决方案1】:

    请用这个在fullname栏搜索:

    $new_csv = array_filter($csv, function ($item) use($SearchThis) {
        //Match the full name
        if ($item[0] === $SearchThis) {
            return true;
        }
        //Split the fullname by space characters into array
        //and see if it contains the $SearchThis 
        return in_array($SearchThis, preg_split("/[\s]+/", $item[0]));
    });
    

    希望对你有帮助。

    【讨论】:

    • 你这个传奇!这正是我所追求的。很有魅力。我也喜欢它的出发方式,因为它对我来说很有意义。谢谢,
    猜你喜欢
    • 2011-08-19
    • 2013-07-14
    • 1970-01-01
    • 2012-07-04
    • 2022-01-03
    • 2012-06-14
    • 2017-10-09
    • 1970-01-01
    • 2014-01-11
    相关资源
    最近更新 更多