2016-07-16 58 views
0

我有一些我写的代码,它工作得很好。除了我不确定我写的是线性搜索还是半身搜索?!我对这些差异感到困惑。有人可以澄清不同之处,我的代码是什么,所以我可以向某人解释它?线性或二进制搜索? -PHP

- 下面的代码搜索用户输入的值。并通过一个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

谢谢迈赫迪!这确实有道理!快速,正是我需要听到的!我无法接受你的回答,因为你回答得太快了哈哈但是我会在几分钟内接受它:-) –

+0

不用担心,我可以帮助:D –