該metaphone案件也可以遵循邁克提出的嚴格情況下相同的結構。
我不認爲需要額外的相似度函數,因爲metaphone的目的應該是爲我們提供一個與聽起來相同的單詞相同的密鑰。
$array1 = array('India','USA','China');
$array2 = array(
'Indiuh is in east',
'United States of America is USA',
'Gandhi was born in India',
'Made in China'
);
$found = array();
foreach ($array1 as $key => $value) {
$found[$value] = preg_grep('/\b'.$value.'\b/i', $array2);
}
var_export($found);
echo "\n\n";
function meta($sentence)
{
return implode(' ', array_map('metaphone', explode(' ', $sentence)));
}
$array2meta = array_map('meta', $array2);
foreach ($array1 as $key => $value) {
$valuemeta = meta($value);
$foundmeta[$value] = preg_grep('/\b'.$valuemeta.'\b/', $array2meta);
$foundmeta[$value] = array_intersect_key($array2, $foundmeta[$value]);
}
var_export($foundmeta);
上面的代碼打印出:
array (
'India' =>
array (
2 => 'Gandhi was born in India',
),
'USA' =>
array (
1 => 'United States of America is USA',
),
'China' =>
array (
3 => 'Made in China',
),
)
array (
'India' =>
array (
0 => 'Indiuh is in east',
2 => 'Gandhi was born in India',
),
'USA' =>
array (
1 => 'United States of America is USA',
),
'China' =>
array (
3 => 'Made in China',
),
)
如何約2的foreach或作爲變體array_walk – Subdigger