我有一些我写的代码,它的效果非常好。除了我不确定我写的是线性还是直觉搜索?!我对这些差异感到非常困惑。有人可以澄清差异和我的代码是什么,所以我可以向别人解释一下吗?
- 下面的代码搜索用户输入的值。并浏览csv数据文件。然后我将所有值保存到一个具有结果的新数组中。希望这是有道理的。
我只是想知道我的代码是线性的还是二进制的?我对他们感到很困惑*
$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
}
答案 0 :(得分:0)
您的代码正在执行线性搜索,二进制搜索要求对数据进行排序,在您的情况下是常规文件,再加上二进制搜索,您从中间开始搜索,并将搜索的值与值进行比较在中间,根据数据的排序方式决定是向左还是向右。 我希望它有所帮助。