搜索包含学生号的名字列表,只输入名字的一部分


Search a list of names with student number with only a part of the name as input

我正在制作一个网页,该网页采用学生编号或学生姓名的一部分,然后搜索学生列表,然后返回学生编号和整个姓名。当用户输入学生号时,我已经完成了。我现在唯一的问题是当用户输入一个名字。我正在考虑使用strpos(),但我不知道下一步该怎么做。

到目前为止我得到的是:

function search_name_list($file, $info)
{
    // get name list array
    $students = get_name_list($file)['students'];
    if (!$students)
    {
        return false;
    }
    // the user searched for the student number
    if (ctype_digit($info))
    {
        // return the name corresponding to the student number if found
        if (array_search($info, $students['snum']))
        {
            return $students['name'][$info];
        }
    }
    // the user searched for the name
    else
    {
        // convert array into string
        $content = implode('|', $students);
        // TODO...
    }
    return false;
}

我希望代码中的注释足够清晰,可以帮助你们理解问题。此外,如果你有建议,使功能更好的整体,你也可以说出来。

编辑:我想我需要一个例子。

例如:数组包含["20111122953","Acosta, Arbyn", "20111112345", "Mayer, Patrick"]。但用户只输入了"Arbyn"或"Acosta"那么我就需要搜索数组中只有我拥有的子字符串。然后返回与我的名字相对应的姓名和学号。

您可以遍历您的结果:

 foreach($students AS $value){
     if(stripos($value, $search) !== false){
         echo 'got '.$value
         break;
     }
 }