Valenok
Новичок
levenshtein()
Всем привет.
Вот как мне вывести при помощи Левинштейна на экран названия файлов в папке в соответствии со схожестью по левинштейну между названием и искомым файлом по убыванию.
Сначала самые похожий - в конце самый не похожий...
Как работает функция левинштейн и как получить все названия файлов в один массив я знаю, но что с ними делать - беспонятия.
надо все название отсортировать в соответствии с левинштейном...
Помогите пожалуйсто.
Название получаю так:
$dir = "dir/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
-~{}~ 05.06.06 20:21:
Тема закрыта. Я воспользовался функцией написанной в коментах на сайте php.net некого [email protected]
Вместо возраста указать значение возвращаемое левинштейном
<?php
//Here is an array example
$test[0]['name'] = "David";
$test[0]['age'] = 28;
$test[1]['name'] = "Dennis";
$test[1]['age'] = 23;
$test[2]['name'] = "Joseph";
$test[2]['age'] = 42;
//Here is the Function
function sortmddata($array, $by, $order, $type){
//$array: the array you want to sort
//$by: the associative array name that is one level deep
////example: name
//$order: ASC or DESC
//$type: num or str
$sortby = "sort$by"; //This sets up what you are sorting by
$firstval = current($array); //Pulls over the first array
$vals = array_keys($firstval); //Grabs the associate Arrays
foreach ($vals as $init){
$keyname = "sort$init";
$$keyname = array();
}
//This was strange because I had problems adding
//Multiple arrays into a variable variable
//I got it to work by initializing the variable variables as arrays
//Before I went any further
foreach ($array as $key => $row) {
foreach ($vals as $names){
$keyname = "sort$names";
$test = array();
$test[$key] = $row[$names];
$$keyname = array_merge($$keyname,$test);
}
}
//This will create dynamic mini arrays so that I can perform
//the array multisort with no problem
//Notice the temp array... I had to do that because I
//cannot assign additional array elements to a
//varaiable variable
if ($order == "DESC"){
if ($type == "num"){
array_multisort($$sortby,SORT_DESC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_DESC, SORT_STRING,$array);
}
} else {
if ($type == "num"){
array_multisort($$sortby,SORT_ASC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_ASC, SORT_STRING,$array);
}
}
//This just goed through and asks the additional arguments
//What they are doing and are doing variations of
//the multisort
return $array;
}
//Now to test it
$test = sortmddata($test,'age','ASC','num');
echo "<pre>";
print_r ($test);
//This will return
//Array (
//[0] => Array ([name] => Dennis [age] => 23 )
//[1] => Array ( [name] => David [age] => 28 )
//[2] => Array ( [name] => Joseph [age] => 42 )
//)
?>
Всем привет.
Вот как мне вывести при помощи Левинштейна на экран названия файлов в папке в соответствии со схожестью по левинштейну между названием и искомым файлом по убыванию.
Сначала самые похожий - в конце самый не похожий...
Как работает функция левинштейн и как получить все названия файлов в один массив я знаю, но что с ними делать - беспонятия.
надо все название отсортировать в соответствии с левинштейном...
Помогите пожалуйсто.
Название получаю так:
$dir = "dir/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
-~{}~ 05.06.06 20:21:
Тема закрыта. Я воспользовался функцией написанной в коментах на сайте php.net некого [email protected]
Вместо возраста указать значение возвращаемое левинштейном
<?php
//Here is an array example
$test[0]['name'] = "David";
$test[0]['age'] = 28;
$test[1]['name'] = "Dennis";
$test[1]['age'] = 23;
$test[2]['name'] = "Joseph";
$test[2]['age'] = 42;
//Here is the Function
function sortmddata($array, $by, $order, $type){
//$array: the array you want to sort
//$by: the associative array name that is one level deep
////example: name
//$order: ASC or DESC
//$type: num or str
$sortby = "sort$by"; //This sets up what you are sorting by
$firstval = current($array); //Pulls over the first array
$vals = array_keys($firstval); //Grabs the associate Arrays
foreach ($vals as $init){
$keyname = "sort$init";
$$keyname = array();
}
//This was strange because I had problems adding
//Multiple arrays into a variable variable
//I got it to work by initializing the variable variables as arrays
//Before I went any further
foreach ($array as $key => $row) {
foreach ($vals as $names){
$keyname = "sort$names";
$test = array();
$test[$key] = $row[$names];
$$keyname = array_merge($$keyname,$test);
}
}
//This will create dynamic mini arrays so that I can perform
//the array multisort with no problem
//Notice the temp array... I had to do that because I
//cannot assign additional array elements to a
//varaiable variable
if ($order == "DESC"){
if ($type == "num"){
array_multisort($$sortby,SORT_DESC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_DESC, SORT_STRING,$array);
}
} else {
if ($type == "num"){
array_multisort($$sortby,SORT_ASC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_ASC, SORT_STRING,$array);
}
}
//This just goed through and asks the additional arguments
//What they are doing and are doing variations of
//the multisort
return $array;
}
//Now to test it
$test = sortmddata($test,'age','ASC','num');
echo "<pre>";
print_r ($test);
//This will return
//Array (
//[0] => Array ([name] => Dennis [age] => 23 )
//[1] => Array ( [name] => David [age] => 28 )
//[2] => Array ( [name] => Joseph [age] => 42 )
//)
?>