1
無法用levenshtein()將我的頭圍繞在這個元素上。不能在單個陣列上使用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()並找到'Ny URL',並且距離當前的'Gammal 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>";
?>
嗯,仍然有問題。看我的編輯。 – Mikael 2014-09-01 19:26:27
@Mikael也許'levenshtein'不是你正在尋找的功能呢?你能給你當前的輸出和期望的輸出嗎? – Sugar 2014-09-01 19:59:32
我可以補充說,當樣本很小時,它可以工作,但是有更多的行會打破,它似乎不會選擇最短的距離。 – Mikael 2014-09-01 20:04:17