【发布时间】:2014-09-01 16:20:37
【问题描述】:
无法使用 levenshtein() 来解决这个问题。
假设我有一个如下所示的数组:
Array
(
[0] => Array
(
[Gammal URL] => /bil-och-garage
[Ny URL] => /catalog/verktyg-och-maskiner
)
[1] => Array
(
[Gammal URL] => /bil-och-garage/12-v-utrustning
[Ny URL] => /catalog/verktyg-och-maskiner/handverktyg
)
[2] => Array
(
[Gammal URL] => /bil-och-garage/12-v-utrustning/antenn
[Ny URL] => /catalog/verktyg-och-maskiner/handverktyg/slag-brytverktyg
)
)
我想要做的是打印数组,但要添加。我想要做的是遍历数组并对每个“Gammal URL”执行 levenshtein() 并找到与当前“Gammal URL”距离最短的“Ny URL”。
如果没有完全匹配 (0),则打印最短的那个。我一直在尝试使用嵌套的 foreach 的不同用途,但无法理解如何一次检查 1 个 url 和数组的其余部分。
简而言之,我想打印上面的整个数组,但第三列的 URL 距离最短。如果以上不是最佳解决方案,也欢迎任何使用两个数组的建议。
编辑
有了这个我仍然得到错误的 URL 作为“匹配的 URL” - 有什么想法吗?
foreach ($import as $key => $arr) {
$shortest = '';
foreach ($import as $key2 => $arr2) {
if ($shortest != '') {
// If the distance between the current Ny URL is shorted than the previously shortest one :
// -> it's the new shortest one, otherwise, I keep the previous one
$shortest = (levenshtein($arr['Gammal URL'], $arr2['Ny URL']) < levenshtein($arr['Gammal URL'], $shortest)) ? $arr2['Ny URL'] : $shortest;
} else { // First attempt is set as the shortest aby default
$shortest = $arr2['Ny URL'];
}
}
// I found the shortest one for that Gammal URL
$import[$key]['shortest'] = $shortest;
echo'<tr>';
echo'<td>'. $arr['Gammal URL']."</td>";
echo'<td>'. $arr['Ny URL'].'</td>';
echo'<td>'. $shortest .'</td>';
}
完整代码
<?php
//debug
ini_set('display_errors', 'On');
error_reporting(E_ALL);
ini_set('auto_detect_line_endings', TRUE);
ini_set('max_execution_time', 300);
?>
<?php
//import
function csv_import($filename='', $delimiter=';')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
$import = csv_import('urler.csv');
//output
//print_r($import);
echo '<table>';
echo '<thead>';
echo '<tr>';
echo "<th>Gammal URL</th>";
echo "<th>Ny URL</th>";
echo "<th>Match URL</th>";
echo "</tr>";
echo "</thead>";
echo "</tbody>";
foreach ($import as $key => $arr) {
$shortest = '';
foreach ($import as $key2 => $arr2) {
if ($shortest != '') {
// If the distance between the current Ny URL is shorted than the previously shortest one :
// -> it's the new shortest one, otherwise, I keep the previous one
$shortest = (levenshtein($arr['Gammal URL'], $arr2['Ny URL']) < levenshtein($arr['Gammal URL'], $shortest)) ? $arr2['Ny URL'] : $shortest;
} else { // First attempt is set as the shortest aby default
$shortest = $arr2['Ny URL'];
}
}
// I found the shortest one for that Gammal URL
$import[$key]['shortest'] = $shortest;
echo'<tr>';
echo'<td>'. $arr['Gammal URL']."</td>";
echo'<td>'. $arr['Ny URL'].'</td>';
echo'<td>'. $shortest .'</td>';
}
echo "</tbody>";
echo "</table>";
?>
【问题讨论】:
标签: php arrays levenshtein-distance